一. 事务注解限制条件

1. 不允许在private方法上面


2. 不能在非事务方法里面调用事务方法


二. 实现机制-异常捕获

Describestransactionattributesonamethodorclass.ThisannotationtypeisgenerallydirectlycomparabletoSpring'sorg.springframework.transaction.interceptor.RuleBasedTransactionAttributeclass,andinfactAnnotationTransactionAttributeSourcewilldirectlyconvertthedatatothelatterclass,sothatSpring'stransactionsupportcodedoesnothavetoknowaboutannotations.Ifnorulesarerelevanttotheexception,itwillbetreatedlikeorg.springframework.transaction.interceptor.DefaultTransactionAttribute(rollingbackonRuntimeExceptionandErrorbutnotoncheckedexceptions).Forspecificinformationaboutthesemanticsofthisannotation'sattributes,consulttheorg.springframework.transaction.TransactionDefinitionandorg.springframework.transaction.interceptor.TransactionAttributejavadocs.

所以如果操作数据库的时候把异常捕获了,那么将不能回滚。


三. 手动回滚

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

所以为了解决这种矛盾,可以捕获异常后,在catch里面手动调用回滚。