关于PROPAGATION_REQUIRED的行为,我很少有问题,但我不能在弹簧文档上澄清。
风景区1:-
@Transactional
method1(){
// do some update without exception
}
当线程从method1中出来时,数据将被提交。对吧?
风景区2:-
@Transactional
method1(){
// do some update without exception
method2();
}
@Transactional
method2(){
// do some update without exception
}
当线程从method1中出来时,数据将被提交。对吧?
风景区3:-
@Transactional
method1(){
// do some update without exception
method2();
}
@Transactional
method2(){
// some update in DB
throw new RunTimeException()
}
什么都不会被赞颂。对吧?
风景区4:-
@Transactional
method1(){
// do some update without exception
method2();
}
@Transactional
method2(){
// some update in DB
throw new SomeCheckedException()
}
当检查异常抛出method1线程时,整个事务将被提交。尽管我可以用@Transactional(rollbackFor=SomeCheckedException.class)
改变这种行为,对吗?
如果上面的理解是正确的,请告诉我。
发布于 2014-12-15 14:09:58
是的,选中的异常不会自动回滚活动事务;只有未选中的RuntimeException
才会回滚。
如果要对某些选中的异常回滚,可以使用@Transactional(rollbackFor=SomeCheckedException.class)
注意:当您在内部调用该方法时,@Transactional
没有任何效果:
method1(){
method2();
}
@Transactional
method2(){
// some update in DB -> fails because there is no transaction
}
只有当Spring可以包装方法调用时,才能应用注释。
https://stackoverflow.com/questions/27484328
复制相似问题