首页 > 其他分享 >Spring管理事务默认回滚的异常是什么?

Spring管理事务默认回滚的异常是什么?

时间:2023-07-24 12:13:37浏览次数:25  
标签:回滚 rollbackOn Spring winner Exception 默认 ex public

问题:

Spring管理事务默认(即没有rollBackFor的情况下)可以回滚的异常是什么?

 

回答:

RuntimeException或者Error。

抛出运行时异常,是否回滚?Yes

@Transactional
public boolean rollbackOn(Throwable ex) {
    return new RuntimeException();
}

抛出错误,是否回滚? Yes

@Transactional
public void testTransationB(){
    throw new Error();
}

抛出非运行时异常,是否回滚? No

@Transactional
public 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的情况就不分析啦,这个也是可以触类旁通的。

 

转载:https://blog.csdn.net/lu930124/article/details/77596045

标签:回滚,rollbackOn,Spring,winner,Exception,默认,ex,public
From: https://www.cnblogs.com/cainiao-Shun666/p/17576877.html

相关文章

  • SpringBoot源码第三章-refreshContext
    refreshContext()刷新上下文privatevoidrefreshContext(ConfigurableApplicationContextcontext){/***cintext=AnnotationConfigApplicationContext*/refresh(context);if(this.registerShutdownHook){ try{ context.registerShu......
  • 《Spring6核心源码解析》已完结,涵盖IOC容器、AOP切面、AOT预编译、SpringMVC,面试杠杠
    作者:冰河星球:http://m6z.cn/6aeFbs博客:https://binghe.gitcode.host文章汇总:https://binghe.gitcode.host/md/all/all.html源码地址:https://github.com/binghe001/spring-annotation-book沉淀,成长,突破,帮助他人,成就自我。大家好,我是冰河~~提起Spring,可以这么说,Spring几乎......
  • 好奇-->springboot启动时候的机制
    问题一:springboot启动的组件顺序?问题二:启动的组件大致分为哪几种?是干嘛的?埋下伏笔,明日解决-->2023-07-24 ......
  • springboot获取ApplicationContext的两种方式
    方法1:启动类返回的就是个ApplicationContext对象,可以把这个对象存在当前类的静态变量中;方法2:写个工具类,实现ApplicationContextAware接口,实现默认方法setApplicationContext,传入的参数即applicationContext,找个地方存放即可......
  • spring6 ioc aop 从入门到精通零基础进阶学习路线?
    当你已经掌握了Spring框架的基础知识以及IoC和AOP的核心概念后,可以进一步深化你的学习。以下是更详细的学习路线:1.IoC容器进阶:-学习如何自定义Bean的初始化和销毁方法,并了解Bean生命周期的各个阶段。-深入了解Spring的作用域(Scope)概念,如单例模式、原型模式、会话模式和请求模......
  • Spring Boot 使用 slf4j 日志
    logback的使用     在学习JAVA闲暇时间翻阅别人的随笔中的过程中,我发现在学习开发中我们经常使用System.out.println()来打印一些信息用来输出日志,但是这样不好,因为这样会在大量的使用System.out的过程中会增加资源的消耗,在查阅相关资料后我发现在实际项目中我们可以使......
  • Springboot 整合mybatis 加导出excel
    快速写一个springboot,mybatis的demo,最后用excel导出。第一步,创建一个新maven命名就按自己的来啦第二步,配置pom文件<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version&g......
  • 2022 javax.management.InstanceNotFoundException: org.springframework.boot:ty
    解决"2022javax.management.InstanceNotFoundException:org.springframework.boot:ty"的步骤对于这个错误,我们需要明确以下几个步骤来解决问题。下面是一个整体的流程表格:步骤描述1确认是否存在相关的InstanceNotFoundException异常2检查org.springframework.boo......
  • SpringBoot自动化装配中,如何解决组件装配顺序
    SpringBoot自动化装配中,如果有两个AutoConfiguration,A依赖B,这时ConditionalOnBean如何保证顺序使需要的Bean会提前加载使用@AutoConfigureAfter,当几个组件加载完成后,再加载当前组件,如:Nacos服务注册自动配置类加载前需要加载:通用的服务注册配置,服务注册的自动化配置,服务发现的自......
  • java spring 异步
    JavaSpring异步实现指南引言在开发过程中,我们经常会遇到一些需要长时间处理的操作,例如网络请求、数据库访问等。为了提高程序的性能和响应速度,我们可以使用异步操作来处理这些耗时的任务。在JavaSpring框架中,提供了多种方式来实现异步操作,本文将介绍如何使用JavaSpring来实现......