当前位置: 首页 > backend >正文

Spring 学习笔记之 @Transactional 异常不回滚汇总

使用springboot时,只要引入spring-jdbc/jpa相关的依赖后,在想要启用事务的方法上加上@Transactional注解就能开启事务,碰到异常就能自动回滚。大大的提高了编码的便捷性性,同时也不侵入代码,保持了代码的简洁性。

默认情况下,Spring时使用的Spring AOP (mode=Mode.Proxy, proxyTargetClass=false)方式启动数据库事务拦截。只有了解清楚了具体背景,才能清除知道事务为什么在碰到异常时没有能够正确回滚。下面是一些常用场景分析:

场景1、未正确配置TransactionManager

使用springboot开发时,引入以下依赖后通常会自动启用TransactionManager。

  • spring-boot-starter-jdbc 是 Spring Boot 提供的用于简化 JDBC(Java Database Connectivity)开发的启动器,引入该依赖后,Spring Boot 会自动配置 DataSourceTransactionManager

  • spring-boot-starter-data-jpa 是 Spring Boot 提供的用于简化 JPA(Java Persistence API)开发的启动器,它集成了 Hibernate 等 JPA 实现框架,方便开发者进行数据库操作。引入该依赖后,Spring Boot 会自动配置 JpaTransactionManager

通过下面代码中的printTransactionManager(TransactionManager transactionManager) 方法可以检查是否配置正常。

@SpringBootApplication
public class MybatisApplication {public static void main(String[] args) {org.springframework.boot.SpringApplication.run(MybatisApplication.class, args);}@BeanObject printTransactionManager(TransactionManager transactionManager) {System.out.println("transactionManager: " + transactionManager);return null;}
}

transactionManager: org.springframework.jdbc.support.JdbcTransactionManager@590765c4 

通过打印语句,可以看到spring中的TransactionManager是否正确配置。

场景2、@Transaction注解不在public方法上

默认情况下,事务是在proxy模式下(即Spring AOP负责拦截事务),proxyTargetClass=false ,有接口的时候使用JDK动态代理实现。没有接口时使用CGLIB进行代理。

JDK代理接口时,都是public方法。

CGLIB代理时,在public方法上能生效。在Spring 6.0 以后,除public方法外,可以代理protected, package-visable修饰的方法。

当@Transactional注解位于private/final修饰的方法上时,事务碰到异常不能正常回滚。

详情参考文档:

Method visibility and @Transactional in proxy mode

The @Transactional annotation is typically used on methods with public visibility. As of 6.0, protected or package-visible methods can also be made transactional for class-based proxies by default. Note that transactional methods in interface-based proxies must always be public and defined in the proxied interface. For both kinds of proxies, only external method calls coming in through the proxy are intercepted.

Using @Transactional :: Spring Framework

示例代码:

@Service
public class UserService {@Autowiredprivate UserRepository userRepository;@Transactionalprivate void createUser(User user) {// 插入用户数据userRepository.save(user);// 可能会抛出异常if (user.getName().equals("error")) {throw new RuntimeException("创建用户失败");}}
}

因为方法的可见性,private修饰的方法不能被代理拦截

场景3、调用内部方法

@Transactional 注解使用 AOP 实现事务管理,而 AOP 是基于代理模式的。当在同一个类内部一个没有事务注解的方法调用有 @Transactional 注解的方法时,事务注解会失效,因为这种调用没有经过代理对象。

示例代码如下:

// 定义 UserService 类,处理用户相关业务逻辑
@Service
class UserService {@Autowiredprivate UserRepository userRepository;// 无事务注解的方法,内部调用有事务注解的方法public void outerMethod() {try {innerMethod();} catch (Exception e) {System.out.println("Exception caught: " + e.getMessage());}}// 有事务注解的方法@Transactionalpublic void innerMethod() {User user = new User("John");userRepository.save(user);// 模拟抛出异常throw new RuntimeException("Simulated exception");}
}

示例代码中,虽然Spring AOP代理了innerMethod方法,但是原始事务不是通过代理的innerMethod进入,而是通过原始类的outerMethod进入,这样就调用的是原始类的innerMethod方法,导致不能进入代理类的innerMethod方法,事务拦截不能生效。

场景4、方法内部Catch了异常

在 Spring 中使用 @Transactional 注解时,如果在方法内部捕获了异常且没有重新抛出,会导致事务无法正常回滚,从而使 @Transactional 注解失效。

@Transactional 注解的事务管理是基于 AOP 实现的,它会在目标方法抛出异常时进行事务回滚。默认情况下,@Transactional 注解只对未被捕获的 RuntimeException 及其子类异常进行回滚操作。如果在方法内部捕获了异常,Spring 就无法感知到异常的抛出,从而不会触发事务回滚逻辑,导致事务继续提交,@Transactional 注解的功能失效。

示例代码

// 定义 UserService 类,处理用户相关业务逻辑
@Service
class UserService {@Autowiredprivate UserRepository userRepository;@Transactionalpublic void createUser() {User user = new User("John");userRepository.save(user);try {// 模拟抛出异常throw new RuntimeException("Simulated exception");} catch (Exception e) {// 捕获异常但未重新抛出System.out.println("Exception caught: " + e.getMessage());}}
}

createUser() 方法:使用 @Transactional 注解标记,在方法内部保存用户信息后抛出一个 RuntimeException 异常,并在 catch 块中捕获该异常,但没有重新抛出。

解决办法:重新抛出异常

在 catch 块中重新抛出异常,让 Spring 能够感知到异常的发生,从而触发事务回滚逻辑。

@Transactional
public void createUser() {User user = new User("John");userRepository.save(user);try {throw new RuntimeException("Simulated exception");} catch (Exception e) {System.out.println("Exception caught: " + e.getMessage());// 重新抛出异常throw e;}
}

场景5、 rollbackFor/rollbackForClassName属性未正确配置

  • 默认回滚规则:@Transactional注解默认只对RuntimeException及其子类和Error进行回滚。若抛出的是受检查异常(如IOExceptionSQLException),默认不会触发回滚。

@Service
class UserService {@Autowiredprivate UserRepository userRepository;@Transactionalpublic void createUser() throws IOException {User user = new User("John");userRepository.save(user);// 抛出受检查异常throw new IOException("Simulated IOException");}
}

IOException是一个CheckedException的子类,不在默认的回滚体系内,所以不能自动回滚。需要使用rollbackFor属性显式指定才能生效。

  • rollbackFor=BaseException.class, 针对BaseException和它的子类回滚。若抛出的异常不在继承体系内,则不能自动回滚。
    @Service
    public class OrderService {@Autowiredprivate OrderRepository orderRepository;@Transactional(rollbackFor = BaseException.class)public void createOrder(Order order) throws Exception {orderRepository.createOrder(order);// will rollback// throw new SubException("出现未知错误");// will not rollbackthrow new OtherException("出现未知错误");}
    }class BaseException extends Exception {public BaseException(String message) {super(message);}
    }class SubException extends BaseException {public SubException(String message) {super(message);}
    }class OtherException extends Exception {public OtherException(String message) {super(message);}
    }

    rollbackForClassName=exceptionPattern, exceptionPattern可以包含异常名字全部或者部分字符。注意这里不是正则表达式,而是基于String.contains(exceptionPattern)来判断的。

    匹配规则的源码如下:

private int getDepth(Class<?> exceptionType, int depth) {if (this.exceptionType != null) {if (this.exceptionType.equals(exceptionType)) {// Found it!return depth;}}else if (exceptionType.getName().contains(this.exceptionPattern)) {// Found it!return depth;}// If we've gone as far as we can go and haven't found it...if (exceptionType == Throwable.class) {return -1;}return getDepth(exceptionType.getSuperclass(), depth + 1);}

不能匹配的示例代码如下:

@Slf4j
@Service
public class OrderService {@Autowiredprivate OrderRepository orderRepository;@Transactional(rollbackForClassName = "Base*Exception")public void createOrder(Order order) throws Exception {orderRepository.createOrder(order);// will not rollbackthrow new Base1Exception("出现未知错误");}
}

 因为exception.typeName="Base1Exception" contains("Base*Exception") 结果未false,所以不匹配,导致不能回滚。

正确的用法如下:

@Service
public class OrderService {@Autowiredprivate OrderRepository orderRepository;@Transactional(rollbackForClassName = "Base")public void createOrder(Order order) throws Exception {orderRepository.createOrder(order);// will rollbackthrow new Base1Exception("出现未知错误");}
}

以上是我过去经常碰到的@Transactional碰到异常不能正常回滚的案例总结,若有遗漏欢迎下方留言。

参考文档:

Declarative Transaction Management :: Spring Framework

http://www.xdnf.cn/news/2278.html

相关文章:

  • 【机器学习-线性回归-3】深入浅出:简单线性回归的概念、原理与实现
  • 【VMware】虚拟机如何扩展存储
  • LLM基础之源码一
  • asammdf 库的依赖项和安装指南
  • 【数据结构】优先级队列
  • 【人工智能之大模型】详述大模型中流水线并行(Pipeline Parallelism)的​GPipe推理框架?
  • 【树莓派 PICO 2 测评】ADC 水位监测系统
  • ZBrush2025.1.3 中文版【ZBrush2025版下载】附安装教程
  • tkinter中Listbox列表框常用的操作方法
  • 单片机-89C51部分:4、固件烧录
  • Pygame多人游戏开发:本地双人对战实战
  • C++篇——继承
  • 详解Adobe Photoshop 2024 下载与安装教程
  • Adruino:人机界面及接口技术
  • SSE协议
  • 飞帆:自定义控件平台
  • 【CF】Day44——Codeforces Round 908 (Div. 2) C + Codeforces Round 1020 (Div. 3) DE
  • PyQt6实例_消息工具_使用与完整代码分享
  • 网络安全于应用服务web中间件服务 默认配置文件的关联(配置漏洞)(完成)
  • 理解计算机系统_网络编程(3)
  • Python循环结构深度解析与高效应用实践
  • 基于STM32定时器中断讲解(HAL库)
  • leetcode66.加一
  • Dubbo(79)Dubbo的监控机制是如何实现的?
  • Python部署Docker报错:curl: (56) Recv failure: Connection reset by peer
  • 零拷贝技术原理的详细解析与java实战方案
  • Java中的final关键字【最通俗易懂】
  • 【Linux网络#1】:网络基础知识
  • Redux基础知识
  • 论文笔记(八十)π0.5: a Vision-Language-Action Model with Open-World Generalization