spring 事务嵌套总结:

spring 事务嵌套:
外层事务TraB,内层事务TraA、TraC


场景1:TraA、TraC @Transactional(默认REQUIRED)
TraB:  traA.update(order1); (traA.update throw new RuntimeException();)
  traC.update(order2);
结果:内外层事务全部回滚;
场景2:TraA、TraC @Transactional(默认REQUIRED)
TraB:  traA.update(order1); (traA.update throw new RuntimeException();try catch traC.update)
  traC.update(order2);
结果:内外层事务全部不回滚,traA中try catch后的事务提交;
场景3:TraA、TraC @Transactional(默认REQUIRED)
TraB:  try{                 (traA.update throw new RuntimeException();在外层TraB try catch TraA)
 traA.update(order1);
 }catch(){}
  traC.update(order2);
结果:内外层事务全部回滚,内层的异常抛出到外层捕获也会回滚;
场景4:TraA @Transactional(propagation=Propagation.REQUIRES_NEW)、TraC @Transactional(默认REQUIRED)
TraB:  
  traA.update(order1); (traA.update throw new RuntimeException();)
  traC.update(order2);
结果:内层事务回滚,外层事务继续提交;
场景5:TraA @Transactional(propagation=Propagation.REQUIRES_NEW)、TraC @Transactional(默认REQUIRED)
TraB:  
  traA.update(order1); (traA.update throw new RuntimeException();try catch traC.update)  
  traC.update(order2);
结果:内外层事务全部不回滚,traA中try catch后的事务提交,达到与场景2的同样效果;
场景6:TraA @Transactional(propagation=Propagation.REQUIRES_NEW)、TraC @Transactional(默认REQUIRED)
TraB:  
  try{ (traA.update throw new RuntimeException();在外层TraB try catch TraA)
traA.update(order1);
  } catch
  traC.update(order2);
结果:内层事务回滚,外层事务不回滚;

猜你喜欢

转载自blog.csdn.net/hansplay/article/details/80409296