23. Spring 事务注解@Transactional和异常捕获
一. 事务注解限制条件
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里面手动调用回滚。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。