问题描述
- Spring 事务嵌套问题 Propagation.REQUIRES_NEW
-
@Transactional(propagation=Propagation.REQUIRED) public void insertEmploye(Employe emp) { this.getSqlSession().insert(""insert"" emp); this.updateDep(emp); throw new Exception(""My Exception""); } @Transactional(propagation=Propagation.REQUIRES_NEW) public void updateDep(Employe emp){ this.getSqlSession().update(""updateCount"" emp); }
请问为什么updatedCount没有更新 Propagation.REQUIRES_NEW 不是启动新的事务 隔离外围的吗?
解决方案
这个update当然不可以了!
第一个insert在一个事务里,插入了记录,但还没提交
然后另一个新事务,重更新刚插入的记录,在这个新事务里,根本无法看到还未提交的insert记录,自然无法更新。
你这个update明显就不应该要求新事务,这样就没问题了。
如果要求新事务,你不等public void insertEmploye(Employe emp)这个方法返回,也就是事务提交到数据库,
再执行update才能更新新插入的记录。
解决方案二:
不明白说的什么意思,是insert成功,没有update么
解决方案三:
如果这两个方法你是写在一个Bean里面的开启事物是REQUIRED,那调用还是REQUIRED,你可以吧REQUIRES_NEW的方法写的另一个service
中。这样才能自治。同时,spring默认是对unchecked事物回滚的。
时间: 2024-08-20 00:40:43