Spring管理事务默认回滚的异常是什么?
来源:互联网 发布:程序源码交易 编辑:程序博客网 时间:2024/05/16 11:05
问题:
Spring管理事务默认(即没有rollBackFor的情况下)可以回滚的异常是什么?
回答:
RuntimeException或者Error。
抛出运行时异常,是否回滚?Yes
@Transactionalpublic boolean rollbackOn(Throwable ex) {return new RuntimeException();}抛出错误,是否回滚?Yes
@Transactionalpublic void testTransationB(){throw new Error();}抛出非运行时异常,是否回滚?No
@Transactionalpublic void testTransationC() throws Exception{throw new Exception();}抛出非运行时异常,有rollBackFor=Exception.class的情况下,是否回滚?Yes
@Transactional(rollbackFor = Exception.class)public void testTransationD() throws Exception{throw new Exception();}
分析:
请看Spring源码类RuleBasedTransactionAttribute:
public boolean rollbackOn(Throwable ex) {if (logger.isTraceEnabled()) {logger.trace("Applying rules to determine whether transaction should rollback on " + ex);}RollbackRuleAttribute winner = null;int deepest = Integer.MAX_VALUE;if (this.rollbackRules != null) {for (RollbackRuleAttribute rule : this.rollbackRules) {int depth = rule.getDepth(ex);if (depth >= 0 && depth < deepest) {deepest = depth;winner = rule;}}}if (logger.isTraceEnabled()) {logger.trace("Winning rollback rule is: " + winner);}// User superclass behavior (rollback on unchecked) if no rule matches.if (winner == null) {logger.trace("No relevant rollback rule found: applying default rules");return super.rollbackOn(ex);}return !(winner instanceof NoRollbackRuleAttribute);}
其中
ex:程序运行中抛出的异常,可以是Exception,RuntimeException,Error;
rollbackRules:代表rollBackFor指定的异常。默认情况下是empty。
所以默认情况下,winner等于null,程序调用super.rollbackOn(ex)
Here is the source code of super.rollbackOn(ex)
public boolean rollbackOn(Throwable ex) {return (ex instanceof RuntimeException || ex instanceof Error);}
真相大白,结论是,默认情况下,如果是RuntimeException或Error的话,就返回True,表示要回滚,否则返回False,表示不回滚。
有rollBackFor的情况就不分析啦,这个也是可以触类旁通的。
博文引用自:http://blog.csdn.net/u012557814/article/details/50685374
抛出错误,是否回滚?Yes
抛出非运行时异常,有rollBackFor=Exception.class的情况下,是否回滚?Yes
阅读全文
0 0
- Spring管理事务默认回滚的异常是什么?
- Spring管理事务默认回滚的异常是什么?
- Spring管理事务默认回滚的异常是什么?
- spring事务默认运行时异常回滚,RuntimeException
- Spring事务异常回滚
- Spring事务异常回滚
- spring 的异常驱动的事务回滚之谜
- Spring中发生异常的事务回滚问题
- Spring异常捕获而且回滚事务的方法
- 实验spring事务回滚与异常的关系
- Spring的AOP事务管理默认针对unchecked异常回滚
- spring异常与事务回滚
- 异常类型 && spring事务回滚
- Spring事务和异常回滚
- Spring事务回滚和异常类
- spring事务管理默认回滚异常
- spring事务的回滚
- Spring的事务回滚
- Codeforces AIM TECH Round 4 (Div 2) 题解 (ABCD)
- 561. Array Partition I
- recycleview 搞效率刷新
- Hibernate知识点概述
- 欢迎使用CSDN-markdown编辑器
- Spring管理事务默认回滚的异常是什么?
- 2017年中OTT运营大数据蓝皮书
- CCF 201604-4 游戏
- JAVA中线程同步的方法(7种)汇总
- Android 单例模式的使用
- jvm源码阅读笔记[2]:你不知道的晋升阈值TenuringThreshold详解
- 小小萌新,刚学指针,写一个指针入门只有一点,哇咔咔
- uva-1476 Error Curves 三分
- matlab2c使用c++实现matlab函数系列教程-rot90函数