Spring 事务失效的 8 种场景!

ops/2025/3/16 15:54:35/

15163cafaea8544ecf8f6a3d9528e6ea.png

在日常工作中,如果对Spring的事务管理功能使用不当,则会造成Spring事务不生效的问题。而针对Spring事务不生效的问题,也是在跳槽面试中被问的比较频繁的一个问题。

点击上方卡片关注我

今天,我们就一起梳理下有哪些场景会导致Spring事务生效。

Spring事务不生效总览

简单来说,Spring事务会在几种特定的场景下失效,如下图所示。

ef054c8842814b1d55f52b0ce31d5944.png

1.数据库不支持事务

Spring事务生效的前提是所连接的数据库要支持事务,如果底层的数据库都不支持事务,则Spring的事务肯定会失效。例如,如果使用的数据库为MySQL,并且选用了MyISAM存储引擎,则Spring的事务就会失效。

2.事务方法未被Spring管理

如果事务方法所在的类没有加载到Spring IOC容器中,也就是说,事务方法所在的类没有被Spring管理,则Spring事务会失效,示例如下。

public?class?ProductService?{
?@Autowired
?private?ProductDao?productDao;?@Transactional(propagation?=?Propagation.REQUIRES_NEW)
?public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??productDao.updateProductStockCountById(stockCount,?id);
?}
}

ProductService类上没有标注@Service注解,Product的实例没有加载到Spring IOC容器中,就会造成updateProductStockCountById()方法的事务在Spring中失效。

3.方法没有被public修饰

如果事务所在的方法没有被public修饰,此时Spring的事务会失效,例如,如下代码所示。

@Service
public?class?ProductService?{
?@Autowired
?private?ProductDao?productDao;?@Transactional(propagation?=?Propagation.REQUIRES_NEW)
?private?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??productDao.updateProductStockCountById(stockCount,?id);
?}
}

虽然ProductService上标注了@Service注解,同时updateProductStockCountById()方法上标注了@Transactional(propagation = Propagation.REQUIRES_NEW)注解。

但是,由于updateProductStockCountById()方法为内部的私有方法(使用private修饰),那么此时updateProductStockCountById()方法的事务在Spring中会失效。

4.同一类中方法调用

如果同一个类中的两个方法分别为A和B,方法A上没有添加事务注解,方法B上添加了 @Transactional事务注解,方法A调用方法B,则方法B的事务会失效。例如,如下代码所示。

@Service
public?class?OrderService?{?@Autowired
?private?OrderDao?orderDao;?@Autowired
?private?ProductDao?productDao;?public?void?submitOrder(){
??//生成订单
??Order?order?=?new?Order();
??long?number?=?Math.abs(new?Random().nextInt(500));
??order.setId(number);
??order.setOrderNo("order_"?+?number);
??orderDao.saveOrder(order);
??//减库存
??this.updateProductStockCountById(1,?1L);
?}?@Transactional(propagation?=?Propagation.REQUIRES_NEW)
?public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??productDao.updateProductStockCountById(stockCount,?id);
?}
}

submitOrder()方法和updateProductStockCountById()方法都在OrderService类中,submitOrder()方法上没有标注事务注解,updateProductStockCountById()方法上标注了事务注解,submitOrder()方法调用了updateProductStockCountById()方法,此时,updateProductStockCountById()方法的事务在Spring中会失效。

5.未配置事务管理器

如果在项目中没有配置Spring的事务管理器,即使使用了Spring的事务管理功能,Spring的事务也不会生效。

例如,没有在项目的配置类中配置如下代码。

@Bean
public?PlatformTransactionManager?transactionManager(DataSource?dataSource)?{
?return?new?DataSourceTransactionManager(dataSource);
}

此时,Spring的事务就会失效。

6.方法的事务传播类型不支持事务

如果内部方法的事务传播类型为不支持事务的传播类型,则内部方法的事务在Spring中会失效。

例如,如下代码所示。

@Service
public?class?OrderService?{
?@Autowired
?private?OrderDao?orderDao;
?@Autowired
?private?ProductDao?productDao;?@Transactional(propagation?=?Propagation.REQUIRED)
?public?void?submitOrder(){
??//生成订单
??Order?order?=?new?Order();
??long?number?=?Math.abs(new?Random().nextInt(500));
??order.setId(number);
??order.setOrderNo("order_"?+?number);
??orderDao.saveOrder(order);
??//减库存
??this.updateProductStockCountById(1,?1L);
?}?@Transactional(propagation?=?Propagation.NOT_SUPPORTED)
?public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??productDao.updateProductStockCountById(stockCount,?id);
?}
}

由于updateProductStockCountById()方法的事务传播类型为NOT_SUPPORTED,不支持事务,则updateProductStockCountById()方法的事务会在Spring中失效。

7.不正确的捕获异常

不正确的捕获异常也会导致Spring的事务失效,示例如下。

@Service
public?class?OrderService?{
?@Autowired
?private?OrderDao?orderDao;
?@Autowired
?private?ProductDao?productDao;?@Transactional(propagation?=?Propagation.REQUIRED)
?public?void?submitOrder(){
??//生成订单
??Order?order?=?new?Order();
??long?number?=?Math.abs(new?Random().nextInt(500));
??order.setId(number);
??order.setOrderNo("order_"?+?number);
??orderDao.saveOrder(order);
??//减库存
??this.updateProductStockCountById(1,?1L);
?}?@Transactional(propagation?=?Propagation.REQUIRED)
?public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
??try{
???productDao.updateProductStockCountById(stockCount,?id);
???int?i?=?1?/?0;
??}catch(Exception?e){
???logger.error("扣减库存异常:",?e.getMesaage());
??}
?}
}

updateProductStockCountById()方法中使用try-catch代码块捕获了异常,即使updateProductStockCountById()方法内部会抛出异常,但也会被catch代码块捕获到,此时updateProductStockCountById()方法的事务会提交而不会回滚,并且submitOrder()方法的事务会提交而不会回滚,这就造成了Spring事务的回滚失效问题。

8.错误的标注异常类型

如果在@Transactional注解中标注了错误的异常类型,则Spring事务的回滚会失效,示例如下。

@Transactional(propagation?=?Propagation.REQUIRED)
public?void?updateProductStockCountById(Integer?stockCount,?Long?id){
?try{
??productDao.updateProductStockCountById(stockCount,?id);
?}catch(Exception?e){
??logger.error("扣减库存异常:",?e.getMesaage());
??throw?new?Exception("扣减库存异常");
?}
}

在updateProductStockCountById()方法中捕获了异常,并且在异常中抛出了Exception类型的异常,此时,updateProductStockCountById()方法事务的回滚会失效。

为何会失效呢?这是因为Spring中对于默认回滚的事务异常类型为RuntimeException,上述代码抛出的是Exception异常。

默认情况下,Spring事务中无法捕获到Exception异常,所以此时updateProductStockCountById()方法事务的回滚会失效。

此时可以手动指定updateProductStockCountById()方法标注的事务异常类型,如下所示。

@Transactional(propagation?=?Propagation.REQUIRED,rollbackFor?=?Exception.class)

这里,需要注意的是:Spring事务注解@Transactional中的rollbackFor属性可以指定 Throwable 异常类及其子类。

好了,今天就到这儿吧,我们下期见~~


http://www.ppmy.cn/ops/166238.html

相关文章

3. 无重复字符的最长子串

给定一个字符串 s ,请你找出其中不含有重复字符的 最长 子串 的长度。 示例 1: 输入: s "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。示例 2: 输入: s "bbbbb" 输出: 1 解释: 因为无…

Linux内核,mmap_pgoff在mmap.c的实现

1. mmap_pgoff的系统调用实现如下 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,unsigned long, prot, unsigned long, flags,unsigned long, fd, unsigned long, pgoff) {return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); }2. ksys_mma…

MyBatis框架操作数据库一>xml和动态Sql

目录 配置连接字符串和MyBatis:数据库的连接配置:XML的配置: XML编写Sql:model层:mapper层: 动态Sql:if 标签和trim标签:where标签:Set标签:Foreach标签: Mybatis的开发有两种方式:: 注解和XML&…

FPGA前端设计适合哪些人学?该怎么学?

FPGA前端设计是一个具有挑战性且薪资待遇优渥的岗位,主要涉及FPGA芯片定义、逻辑结构设计。这个职位要求相关专业的本科及以上学历,并且需要掌握一定的专业技能。工作内容从IP级设计到全芯片(SoC)设计,涉及多个设计层级…

iOS应用程序开发(图片处理器)

续上篇 iOS 编程开发图片浏览器,继续实现一个图标生成功能。 功能。 操作系统平台:MacBook(macOS) IDE:xcode 编程语言:Objective-c 以下是小程序在 iPhone 模拟器中的运行视频。也可以在 iPad 模拟器中运行。 效果图如下所示&#xff1a…

Linux find 命令完全指南

find 是 Linux 系统最强大的文件搜索工具&#xff0c;支持 嵌套遍历、条件筛选、执行动作。以下通过场景分类解析核心用法&#xff0c;涵盖高效搜索、文件管理及高级技巧&#xff1a; 一、基础搜索模式 1. 按文件名搜索&#xff08;精确/模糊匹配&#xff09; <BASH> f…

洛谷 P1725 琪露诺 单调队列优化的线性dp

以上是题目 考虑到2e5的数据范围&#xff0c;暴力的先枚举i&#xff0c;在枚举走的步数区间j&#xff0c;是过不了的&#xff0c; 我们可以看出对于每一个i&#xff0c;只需要找出能走的i的区间的dp最大值即可&#xff0c;求区间最大值可以使用单调队列&#xff0c;时间复杂度…

江科大51单片机笔记【12】AT24C02(I2C总线)

写在前言 此为博主自学江科大51单片机&#xff08;B站&#xff09;的笔记&#xff0c;方便后续重温知识 在后面的章节中&#xff0c;为了防止篇幅过长和易于查找&#xff0c;我把一个小节分成两部分来发&#xff0c;上章节主要是关于本节课的硬件介绍、电路图、原理图等理论知识…