一. 事务注解限制条件
1. 不允许在private方法上面
2. 不能在非事务方法里面调用事务方法
二. 实现机制-异常捕获
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.
所以如果操作数据库的时候把异常捕获了,那么将不能回滚。
三. 手动回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
所以为了解决这种矛盾,可以捕获异常后,在catch里面手动调用回滚。
时间: 2024-10-02 01:09:01