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

@Transactional 注解怎么用?

接手新项目一言难尽,别的不说单单就一个@Transactional注解用的一塌糊涂,五花八门的用法,很大部分还失效无法回滚。

有意识的在涉及事务相关方法上加@Transactional注解,是个好习惯。不过,很多同学只是下意识地添加这个注解,一旦功能正常运行,很少有人会深入验证异常情况下事务是否能正确回滚。@Transactional 注解虽然用起来简单,但这货总是能在一些你意想不到的情况下失效,防不胜防!

我把这些事务问题归结成了三类:不必要不生效不回滚,接下用一些demo演示下各自的场景。

不必要

1. 无需事务的业务

在没有事务操作的业务方法上使用 @Transactional 注解,比如:用在仅有查询或者一些 HTTP 请求的方法,虽然加上影响不大,但从编码规范的角度来看还是不够严谨,建议去掉。

@Transactional public String testQuery() { standardBak2Service.getById(1L); return "testB"; }

2. 事务范围过大

有些同学为了省事直接将 @Transactional 注解加在了类上或者抽象类上,这样做导致的问题就是类内的方法或抽象类的实现类中所有方法全部都被事务管理。增加了不必要的性能开销或复杂性,建议按需使用,只在有事务逻辑的方法上加@Transactional。

@Transactional public abstract class BaseService { } @Slf4j @Service public class TestMergeService extends BaseService{ private final TestAService testAService; public String testMerge() { testAService.testA(); return "ok"; } }

如果在类中的方法上添加 @Transactional 注解,它将覆盖类级别的事务配置。例如,类级别上配置了只读事务,方法级别上的 @Transactional 注解也会覆盖该配置,从而启用读写事务。

@Transactional(readOnly = true) public class TestMergeService { private final TestBService testBService; private final TestAService testAService; @Transactional public String testMerge() { testAService.testA(); testBService.testB(); return "ok"; } }

不生效

3. 方法权限问题

不要把 @Transactional注解加在 private 级别的方法上!

我们知道 @Transactional 注解依赖于Spring AOP切面来增强事务行为,这个 AOP 是通过代理来实现的,而 private 方法恰恰不能被代理的,所以 AOP 对 private 方法的增强是无效的,@Transactional也就不会生效。

@Transactional private String testMerge() { testAService.testA(); testBService.testB(); return "ok"; }

那如果我在 testMerge() 方法内调用 private 的方法事务会生效吗?

答案:事务会生效

@Transactional public String testMerge() throws Exception { ccc(); return "ok"; } private void ccc() { testAService.testA(); testBService.testB(); }

4. 被用 final 、static 修饰方法

和上边的原因类似,被用finalstatic修饰的方法上加 @Transactional 也不会生效。

  • static 静态方法属于类本身的而非实例,因此代理机制是无法对静态方法进行代理或拦截的

  • final 修饰的方法不能被子类重写,事务相关的逻辑无法插入到 final 方法中,代理机制无法对 final 方法进行拦截或增强。

这些都是java基础概念了,使用时要注意。

@Transactional public static void b() { } @Transactional public final void b() { }

5. 同类内部方法调用问题

注意了,这种情况经常发生啊!

同类内部方法间的调用是 @Transactional 注解失效的重灾区,网上你总能看到方法内部调用另一个同类的方法时,这种调用是不会经过代理的,因此事务管理不会生效。但这说法比较片面,要分具体情况。

比如:testMerge() 方法开启事务,调用同类非事务的方法 a() 和 b() ,此时 b() 抛异常,根据事务的传播性 a()、b() 事务均生效。

@Transactional public String testMerge() { a(); b(); return "ok"; } public void a() { standardBakService.save(testAService.buildEntity()); } public void b() { standardBak2Service.save(testBService.buildEntity2()); throw new RuntimeException("b error"); }

如果 testMerge() 方法未开启事务,并且在同类中调用了非事务方法 a() 和事务方法 b(),当 b() 抛出异常时,a() 和 b() 的事务都不会生效。因为这种调用直接通过this对象进行,未经过代理,因此事务管理无法生效。这经常出问题的!

public String testMerge() { a(); b(); return "ok"; } public void a() { standardBakService.save(testAService.buildEntity()); } @Transactional public void b() { standardBak2Service.save(testBService.buildEntity2()); throw new RuntimeException("b error"); }
5.1 独立的 Service 类

要想 b() 方法的事务生效也容易,最简单的方法将它剥离放在独立的Service类注入使用,交给spring管理就行了。不过,这种方式会创建很多类。

@Slf4j @Service public class TestBService { @Transactional public void b() { standardBak2Service.save(testBService.buildEntity2()); throw new RuntimeException("b error"); } }
5.2 自注入方式

或者通过自己注入自己的方式解决,尽管解决了问题,逻辑看起来很奇怪,它破坏了依赖注入的原则,虽然 spring 支持我们这样用,还是要注意下循环依赖的问题。

@Slf4j @Service public class TestMergeService { @Autowired private TestMergeService testMergeService; public String testMerge() { a(); testMergeService.b(); return "ok"; } public void a() { standardBakService.save(testAService.buildEntity()); } @Transactional public void b() { standardBak2Service.save(testBService.buildEntity2()); throw new RuntimeException("b error"); } }
5.3 手动获取代理对象

b() 方法它不是没被代理嘛,那我们手动获取代理对象调用 b() 方法也可以。通过AopContext.currentProxy()方法返回当前的代理对象实例,这样调用代理的方法时,就会经过 AOP 的切面,@Transactional注解就会生效了。

@Slf4j @Service public class TestMergeService { public String testMerge() { a(); ((TestMergeService) AopContext.currentProxy()).b(); return "ok"; } public void a() { standardBakService.save(testAService.buildEntity()); } @Transactional public void b() { standardBak2Service.save(testBService.buildEntity2()); throw new RuntimeException("b error"); } }

6. Bean 未被 spring 管理

上边我们知道 @Transactional 注解通过 AOP 来管理事务,而 AOP 依赖于代理机制。因此,Bean 必须由Spring管理实例!要确保为类加上如@Controller@Service@Component注解,让其被Spring所管理,这很容易忽视。

@Service public class TestBService { @Transactional public String testB() { standardBak2Service.save(entity2); return "testB"; } }

7. 异步线程调用

如果我们在 testMerge() 方法中使用异步线程执行事务操作,通常也是无法成功回滚的,来个具体的例子。

testMerge() 方法在事务中调用了 testA(),testA() 方法中开启了事务。接着,在 testMerge() 方法中,我们通过一个新线程调用了 testB(),testB() 中也开启了事务,并且在 testB() 中抛出了异常。

此时的回滚情况是怎样的呢?

@Transactional public String testMerge() { testAService.testA(); new Thread(() -> { try { testBService.testB(); } catch (Exception e) { // e.printStackTrace(); throw new RuntimeException(); } }).start(); return "ok"; } @Transactional public String testB() { DeepzeroStandardBak2 entity2 = buildEntity2(); dataImportJob2Service.save(entity2); throw new RuntimeException("test2"); } @Transactional public String testA() { DeepzeroStandardBak entity = buildEntity(); standardBakService.save(entity); return "ok"; }

答案是:testA() 不回滚、 testB() 回滚。

testA() 无法回滚是因为没有捕获到新线程中 testB()抛出的异常;testB()方法可以回滚,是因为事务管理器只对当前线程中的事务有效,因此在新线程中执行的事务会回滚。

由于在多线程环境下,Spring 的事务管理器不会跨线程传播事务,事务的状态(如事务是否已开启)是存储在线程本地的ThreadLocal来存储和管理事务上下文信息。这意味着每个线程都有一个独立的事务上下文,事务信息在不同线程之间不会共享。

8. 不支持事务的引擎

不支持事务的数据库引擎不在此次Review范围内,只做了解就好。我们通常使用的关系型数据库,如 MySQL,默认使用支持事务的InnoDB引擎,而非事务的MyISAM引擎则使用较少。

以前开启启用 MyISAM 引擎是为了提高查询效率。不过,现在非关系型数据库如RedisMongoDBElasticsearch等中间件提供了更高性价比的解决方案。

不回滚

9. 用错传播属性

@Transactional注解有个关键的参数propagation,它控制着事务的传播行为,有时事务传播参数配置错误也会导致事务的不回滚。

propagation 支持 7 种事务传播特性:

  • REQUIRED默认的传播行为,如果当前没有事务,则创建一个新事务;如果存在事务,则加入当前事务。

  • MANDATORY:支持当前事务,如果不存在则抛出异常

  • NEVER:非事务性执行,如果存在事务,则抛出异常

  • REQUIRES_NEW:无论当前是否存在事务,都会创建一个新事务,原有事务被挂起。

  • NESTED:嵌套事务,被调用方法在一个嵌套的事务中运行,这个事务依赖于当前的事务。

  • SUPPORTS:如果当前存在事务,则加入;如果没有,就以非事务方式执行。

  • NOT_SUPPORTED:以非事务方式执行,如果当前存在事务,将其挂起。

为了加深印象,我用案例来模拟下每种特性的使用场景。

REQUIRED

REQUIRED 是默认的事务传播行为。如果 testMerge() 方法开启了事务,那么其内部调用的 testA() 和 testB() 方法也将加入这个事务。如果 testMerge() 没有开启事务,而 testA() 和 testB() 方法上使用了 @Transactional 注解,这些方法将各自创建新的事务,只控制自身的回滚。

@Component @RequiredArgsConstructor @Slf4j @Service public class TestMergeService { private final TestBService testBService; private final TestAService testAService; @Transactional public String testMerge() { testAService.testA(); testBService.testB(); return "ok"; } } @Transactional public String testA() { log.info("testA"); DeepzeroStandardBak entity = buildEntity(); standardBakService.save(entity); return "ok"; } @Transactional public String testB() { log.info("testB"); DeepzeroStandardBak2 entity2 = buildEntity2(); standardBak2Service.save(entity2); throw new RuntimeException("testB"); }
MANDATORY

MANDATORY 传播特性简单来说就是只能被开启事务的上层方法调用,例如 testMerge() 方法未开启事务调用 testB() 方法,那么将抛出异常;testMerge() 开启事务调用 testB() 方法,则加入当前事务。

@Component @RequiredArgsConstructor @Slf4j @Service public class TestMergeService { private final TestBService testBService; private final TestAService testAService; public String testMerge() { testAService.testA(); testBService.testB(); return "ok"; } } @Transactional public String testA() { log.info("testA"); DeepzeroStandardBak entity = buildEntity(); standardBakService.save(entity); return "ok"; } @Transactional(propagation = Propagation.MANDATORY) public String testB() { log.info("testB"); DeepzeroStandardBak2 entity2 = buildEntity2(); standardBak2Service.save(entity2); throw new RuntimeException("testB"); }

抛出的异常信息

org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'

NEVER

NEVER 传播特性是强制你的方法只能以非事务方式运行,如果方法存在事务操作会抛出异常,我实在是没想到有什么使用场景。

@Transactional(propagation = Propagation.NEVER) public String testB() { log.info("testB"); DeepzeroStandardBak2 entity2 = buildEntity2(); standardBak2Service.save(entity2); // throw new RuntimeException("testB"); return "ok"; }

抛出的异常信息

org.springframework.transaction.IllegalTransactionStateException: Existing transaction found for transaction marked with propagation 'never'

REQUIRES_NEW

我们在使用 Propagation.REQUIRES_NEW 传播特性时,不论当前事务的状态如何,调用该方法都会创建一个新的事务。

例如,testMerge() 方法开始一个事务,调用 testB() 方法时,它会暂停 testMerge() 的事务,并启动一个新的事务。如果 testB() 方法内部发生异常,新事务会回滚,但原先挂起的事务不会受影响。这意味着,挂起的事务不会因为新事务的回滚而受到影响,也不会因为新事务的失败而回滚。

@Transactional public String testMerge() { testAService.testA(); testBService.testB(); return "ok"; } @Transactional public String testA() { log.info("testA"); DeepzeroStandardBak entity = buildEntity(); standardBakService.save(entity); return "ok"; } @Transactional(propagation = Propagation.REQUIRES_NEW) public String testB() { log.info("testB"); DeepzeroStandardBak2 entity2 = buildEntity2(); standardBak2Service.save(entity2); throw new RuntimeException("testB"); }
NESTED

方法的传播行为设置为 NESTED,其内部方法会开启一个新的嵌套事务(子事务)。在没有外部事务的情况下NESTEDREQUIRED效果相同;存在外部事务的情况下,一旦外部事务回滚,它会创建一个嵌套事务(子事务)。

也就是说外部事务回滚时,子事务会跟着回滚;但子事务的回滚不会对外部事务和其他同级事务造成影响。

@Component @RequiredArgsConstructor @Slf4j @Service public class TestMergeService { private final TestBService testBService; private final TestAService testAService; @Transactional public String testMerge() { testAService.testA(); testBService.testB(); throw new RuntimeException("testMerge"); return "ok"; } } @Transactional public String testA() { log.info("testA"); DeepzeroStandardBak entity = buildEntity(); standardBakService.save(entity); return "ok"; } @Transactional(propagation = Propagation.NESTED) public String testB() { log.info("testB"); DeepzeroStandardBak2 entity2 = buildEntity2(); standardBak2Service.save(entity2); throw new RuntimeException("testB"); }
NOT_SUPPORTED

NOT_SUPPORTED事务传播特性表示该方法必须以非事务方式运行。当方法 testMerge() 开启事务并调用事务方法 testA() 和 testB() 时,如果 testA() 和 testB() 的事务传播特性为 NOT_SUPPORTED,那么 testB() 将以非事务方式运行,并挂起当前的事务。

默认传播特性的情况下 testB() 异常事务加入会导致 testA() 回滚,而挂起的意思是说,testB() 其内部一旦抛出异常,不会影响 testMerge() 中其他 testA() 方法的回滚。

@Component @RequiredArgsConstructor @Slf4j @Service public class TestMergeService { private final TestBService testBService; private final TestAService testAService; @Transactional public String testMerge() { testAService.testA(); testBService.testB(); return "ok"; } } @Transactional public String testA() { log.info("testA"); DeepzeroStandardBak entity = buildEntity(); standardBakService.save(entity); return "ok"; } @Transactional(propagation = Propagation.NOT_SUPPORTED) public String testB() { log.info("testB"); DeepzeroStandardBak2 entity2 = buildEntity2(); standardBak2Service.save(entity2); throw new RuntimeException("testB"); }
SUPPORTS

如果当前方法的事务传播特性是SUPPORTS,那么只有在调用该方法的上层方法开启了事务的情况下,该方法的事务才会有效。如果上层方法没有开启事务,那么该方法的事务特性将无效。

例如,如果入口方法 testMerge() 没有开启事务,而 testMerge() 调用的方法 testA() 和 testB() 的事务传播特性为 SUPPORTS,那么由于 testMerge() 没有事务,testA() 和 testB() 将以非事务方式执行。即使在这些方法上加上@Transactional注解,也不会回滚异常。

@Component @RequiredArgsConstructor @Slf4j @Service public class TestMergeService { private final TestBService testBService; private final TestAService testAService; public String testMerge() { testAService.testA(); testBService.testB(); return "ok"; } } @Transactional(propagation = Propagation.SUPPORTS) public String testA() { log.info("testA"); DeepzeroStandardBak entity = buildEntity(); standardBakService.save(entity); return "ok"; } @Transactional(propagation = Propagation.SUPPORTS) public String testB() { log.info("testB"); DeepzeroStandardBak2 entity2 = buildEntity2(); standardBak2Service.save(entity2); throw new RuntimeException("testB"); }

10. 自己吞了异常

在整个 review 的过程中我发现导致事务不回滚的场景,多数是开发同学在业务代码中手动 try...catch 捕获了异常,然后又没抛出异常....

比如:testMerge() 方法开启了事务,并调用了非事务方法 testA() 和 testB(),同时在 testMerge() 中捕获了异常。如果 testB() 中发生了异常并抛出,但 testMerge() 捕获了这个异常而没有继续抛出,Spring 事务将无法捕获到异常,从而无法进行回滚。

@RequiredArgsConstructor @Slf4j @Service public class TestMergeService { private final TestBService testBService; private final TestAService testAService; @Transactional public String testMerge() { try { testAService.testA(); testBService.testB(); } catch (Exception e) { log.error("testMerge error:{}", e); } return "ok"; } } @Service public class TestAService { public String testA() { standardBakService.save(entity); return "ok"; } } @Service public class TestBService { public String testB() { standardBakService.save(entity2); throw new RuntimeException("test2"); } }

为了确保 Spring 事务能够正常回滚,需要我们在 catch 块中主动重新抛出它能够处理的 RuntimeException 或者 Error 类型的异常。

@Transactional public String testMerge() { try { testAService.testA(); testBService.testB(); } catch (Exception e) { log.error("testMerge error:{}", e); throw new RuntimeException(e); } return "ok"; }

捕获异常并不意味着一定不会回滚,这取决于具体情况。

例如,当 testB() 方法上也加上了 @Transactional 注解时,如果在该方法中发生异常,事务会捕获到这个异常。由于事务传播的特性,testB() 的事务会合并到上层方法的事务中。因此,即使在 testMerge() 中捕获了异常而未抛出,事务仍然可以成功回滚。

@Transactional public String testB() { DeepzeroStandardBak2 entity2 = buildEntity2(); dataImportJob2Service.save(entity2); throw new RuntimeException("test2"); // return "ok"; }

但这有个提前,必须在 testMerge() 方法上添加 @Transactional 注解以启用事务。如果 testMerge() 方法没有开启事务,不论其内部是否使用 try 块,都只能部分回滚 testB(),而 testA() 将无法回滚。

11. 事务无法捕获的异常

Spring 的事务默认会回滚RuntimeException及其子类,以及Error类型的异常。

如果抛出的是其他类型的异常,例如checked exceptions(检查型异常),即继承自 Exception 但不继承自 RuntimeException 的异常,比如SQLExceptionDuplicateKeyException,事务将不会回滚。

所以,我们在主动抛出异常时,要确保该异常是事务能够捕获的类型。

@Transactional public String testMerge() throws Exception { try { testAService.testA(); testBService.testB(); } catch (Exception e) { log.error("testMerge error:{}", e); // throw new RuntimeException(e); throw new Exception(e); } return "ok"; }

如果你非要抛出默认情况下不会导致事务回滚的异常,务必要在@Transactional注解的rollbackFor参数中明确指定该异常,这样才能进行回滚。

@Transactional(rollbackFor = Exception.class) public String testMerge() throws Exception { try { testAService.testA(); testBService.testB(); } catch (Exception e) { log.error("testMerge error:{}", e); // throw new RuntimeException(e); throw new Exception(e); } return "ok"; }

问问你身边的同学,哪些异常属于运行时异常,哪些属于检查型异常,十有八九他们可能无法给出准确的回答!

所以减少出现 bug 的风险,我建议使用 @Transactional 注解时,将 rollbackFor 参数设置为ExceptionThrowable,这样可以扩大事务回滚的范围。

12. 自定义异常范围问题

针对不同业务定制异常类型是比较常见的做法,@Transactional 注解的 rollbackFor 参数支持自定义的异常,但我们往往习惯于将这些自定义异常继承自 RuntimeException。

那么这就出现和上边同样的问题,事务的范围不足,许多异常类型仍然无法触发事务回滚。

@Transactional(rollbackFor = CustomException.class) public String testMerge() throws Exception { try { testAService.testA(); testBService.testB(); } catch (Exception e) { log.error("testMerge error:{}", e); // throw new RuntimeException(e); throw new Exception(e); } return "ok"; }

想要解决这个问题,可以在 catch 中主动抛出我们自定义的异常。

@Transactional(rollbackFor = CustomException.class) public String testMerge() throws Exception { try { testAService.testA(); testBService.testB(); } catch (Exception e) { log.error("testMerge error:{}", e); throw new CustomException(e); } return "ok"; }

13. 嵌套事务问题

还有一种场景就是嵌套事务问题,比如,我们在 testMerge() 方法中调用了事务方法 testA() 和事务方法 testB(),此时不希望 testB() 抛出异常让整个 testMerge() 都跟着回滚;这就需要单独 try catch 处理 testB() 的异常,不让异常在向上抛。

@RequiredArgsConstructor @Slf4j @Service public class TestMergeService { private final TestBService testBService; private final TestAService testAService; @Transactional public String testMerge() { testAService.testA(); try { testBService.testB(); } catch (Exception e) { log.error("testMerge error:{}", e); } return "ok"; } } @Service public class TestAService { @Transactional public String testA() { standardBakService.save(entity); return "ok"; } } @Service public class TestBService { @Transactional public String testB() { standardBakService.save(entity2); throw new RuntimeException("test2"); } }
http://www.jsqmd.com/news/1379472/

相关文章:

  • 火狐渗透版禁用JS破解CTF无限刷新难题
  • 霍邱中央空调品牌推荐:松下纳诺怡除醛+自有团队安装,8年服务近2000户的本地之选 - 滚动商讯
  • TrollInstallerX终极指南:iOS 14-16.6.1系统漏洞利用与TrollStore安装器
  • 第19届成图大赛国赛复盘:从CAD建模到创新设计的实战指南
  • 猫抓浏览器扩展:你的智能网页资源管家,轻松捕获一切在线媒体
  • 非线性磁链模型在无感FOC中的应用:原理、实现与开源库实战
  • Systemd 学习总结
  • 2026鲜蛋桃酥哪个牌子好?全品类正规品牌盘点、服务商选型标准及合作避坑指南 - 商业大观
  • 彻底解决Chrome WebDriver进程残留:从原理到实战的完整指南
  • 技术项目管理新思路:黑马白马分队策略如何激发团队创新与效率
  • 华为模拟器 eNSP 详细安装步骤(附常见问题解决)
  • 襄阳本地防水维修科普:漏水原因、施工方案与选择建议 - 筑宅安
  • 一念成仙机器人到底是什么?当一个机器有了头衔和卡片,它就变成游戏了吗?
  • 从一次工资字段读取说起,彻底理解 SAP Read Access Logging 与 OData V2 的审计边界
  • 痛风饮食安全算法:精准管理海鲜嘌呤摄入
  • 5分钟快速上手TronWeb:TRON区块链开发的终极指南
  • 挂号咨询相关问题解答 - GrowthUME
  • PDSCH接收链怎样验证?从EVM、LLR校准到BLER定位
  • AI Agent学习:RAG基础:构建Agent知识获取管道(李博杰《深入理解 AI Agent》3.2观后总结)
  • 广州先进封装半导体厂家推荐哪家好?2026年先进封装设备企业实力解析! - 趣谈科技事物
  • 从零构建MCP Server:为LangChain Agent扩展本地文件读取能力
  • 2026年8月南京夜校机构深度测评,选型指南 - 甄选测评馆
  • 深入解析Agent持续执行机制:如何避免任务中途停止
  • 选对工具少走弯路!2026高性价比国产DFM软件推荐来了 - 2027品牌AI展
  • 3种方法彻底告别安装:lessmsi让Windows安装包提取变得如此简单!
  • Spring Boot图片代理接口实战:彻底解决前端外链图片跨域与403问题
  • 砼匠无人值守系统适合多大产能的搅拌站?不同规模厂区适配分析
  • JMeter压力测试实战:从环境搭建到结果分析的全流程指南
  • Wand-Enhancer:免费解锁WeMod专业版的终极解决方案指南
  • 从游戏新手到模组大神:BepInEx零基础入门完全指南