标签:spring
一. 事务注解限制条件
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里面手动调用回滚。
本文出自 “www.bogo.com” 博客,请务必保留此出处http://483181.blog.51cto.com/473181/1937481
23. Spring 事务注解@Transactional和异常捕获
标签:spring
原文地址:http://483181.blog.51cto.com/473181/1937481