一. 事务注解限制条件
1. 不允许在private方法上面
2. 不能在非事务方法里面调用事务方法
二. 实现机制-异常捕获
1
2
3
4
5
6
|
Describes transaction attributes on a method or class.
This annotation type is generally directly comparable to Spring's org.springframework.transaction.interceptor.RuleBasedTransactionAttribute class, and in fact AnnotationTransactionAttributeSource will directly convert the data to the latter class, so that Spring's transaction
support code does not have to know about annotations. If no rules are relevant
to the exception, it will be treated like org.springframework.transaction.interceptor.DefaultTransactionAttribute (rolling back on RuntimeException and Error but not on checked exceptions).
For specific information about the semantics of this annotation's attributes,
consult the org.springframework.transaction.TransactionDefinition and org.springframework.transaction.interceptor.TransactionAttribute javadocs.
|
所以如果操作数据库的时候把异常捕获了,那么将不能回滚。
三. 手动回滚
1
|
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
所以为了解决这种矛盾,可以捕获异常后,在catch里面手动调用回滚。
本文转自rongwei84n 51CTO博客,原文链接:http://blog.51cto.com/483181/1937481,如需转载请自行联系原作者