在做SSH整合add方法的时候运行报错:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
其实这个异常的提示还是很明确的:在只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。
尝试了在单独的方法上面加以下,失败了
@Transactional(readOnly = false)就算我在单独方法那写,我调的父方法是BaseHibernateDao里面的,还就得在BaseHibernate父方法写:
@Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW)
在方法中加上getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
即:
public void addUser(User user){
getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
getHibernateTemplate().save(user);
}然后问题来了,直接报错:
Could not obtain transaction-synchronized Session for current thread
提示无法获取当前线程的事务同步session,略微奇怪,这和事务有什么关系..然后百度一下有人说改成用openSession方法就好了,那我又百度了一下这2个方法的区别:
(1)openSession每次打开都是新的Session,所以多次获取的Session实例是不同的,并且需要人为的调用close方法进行Session关闭。
(2)getCurrentSession是从当前上下文中获取Session并且会绑定到当前线程,第一次调用时会创建一个Session实例,如果该Session未关闭,后续多次获取的是同一个Session实例;事务提交或者回滚时会自动关闭Sesison,无需人工关闭。
看起来这个getCurrentSession方法的确和事务有点关系.然后我在applicationContext.xml加上事务:
<tx:annotation-driven transaction-manager="transactionManager"/>然后在BaseHibernateDao层加上@Transactional注解,再运行ok了。
本文介绍了解决在SSH整合中add方法运行时出现的InvalidDataAccessApiUsageException异常的方法。通过调整FlushMode设置及使用openSession替代getCurrentSession解决获取Session的问题,并通过在BaseHibernateDao层添加@Transactional注解配合配置文件中的事务管理实现正常运行。
167

被折叠的 条评论
为什么被折叠?



