hibernate事务解决问题

在做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了。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值