spring事务失效的12种场景

对于从事 java 开发工作的同学来说,spring 的事务肯定再熟悉不过了。

在某些业务场景下,如果一个请求中,需要同时写入多张表的数据。为了保证操作的原子性(要么同时成功,要么同时失败),避免数据不一致的情况,我们一般都会用到 spring 事务。

确实,spring 事务用起来贼爽,就用一个简单的注解:@Transactional,就能轻松搞定事务。我猜大部分小伙伴也是这样用的,而且一直用一直爽。

但如果你使用不当,它也会坑你于无形。

图片

一、事务不生效

 1.访问权限问题

众所周知,java 的访问权限主要有四种:private、default、protected、public,它们的权限从左到右,依次变大。

但如果我们在开发过程中,把某些事务方法,定义了错误的访问权限,就会导致事务功能出问题,例如:


   
   
  1. @Service
  2. public class UserService {
  3. @Transactional
  4. private void add (UserModel userModel) {
  5. saveData(userModel);
  6. updateData(userModel);
  7. }
  8. }

我们可以看到 add 方法的访问权限被定义成了private,这样会导致事务失效,spring 要求被代理方法必须是public的。

说白了,在AbstractFallbackTransactionAttributeSource类的computeTransactionAttribute方法中有个判断,如果目标方法不是 public,则TransactionAttribute返回 null,即不支持事务。


   
   
  1. protected TransactionAttribute computeTransactionAttribute (Method method, @Nullable Class<?> targetClass) {
  2. // Don't allow no-public methods as required.
  3. if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
  4. return null;
  5. }
  6. // The method may be on an interface, but we need attributes from the target class.
  7. // If the target class is null, the method will be unchanged.
  8. Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
  9. // First try is the method in the target class.
  10. TransactionAttribute txAttr = findTransactionAttribute(specificMethod);
  11. if (txAttr != null) {
  12. return txAttr;
  13. }
  14. // Second try is the transaction attribute on the target class.
  15. txAttr = findTransactionAttribute(specificMethod.getDeclaringClass());
  16. if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
  17. return txAttr;
  18. }
  19. if (specificMethod != method) {
  20. // Fallback is to look at the original method.
  21. txAttr = findTransactionAttribute(method);
  22. if (txAttr != null) {
  23. return txAttr;
  24. }
  25. // Last fallback is the class of the original method.
  26. txAttr = findTransactionAttribute(method.getDeclaringClass());
  27. if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
  28. return txAttr;
  29. }
  30. }
  31. return null;
  32. }

也就是说,如果我们自定义的事务方法(即目标方法),它的访问权限不是public,而是 private、default 或 protected 的话,spring 则不会提供事务功能。

2. 方法用 final 修饰 

有时候,某个方法不想被子类重写,这时可以将该方法定义成 final 的。普通方法这样定义是没问题的,但如果将事务方法定义成 final,例如:


   
   
  1. @Service
  2. public class UserService {
  3. @Transactional
  4. public final void add (UserModel userModel){
  5. saveData(userModel);
  6. updateData(userModel);
  7. }
  8. }

我们可以看到 add 方法被定义成了final的,这样会导致事务失效。

为什么?

如果你看过 spring 事务的源码,可能会知道 spring 事务底层使用了 aop,也就是通过 jdk 动态代理或者 cglib,帮我们生成了代理类,在代理类中实现的事务功能。

但如果某个方法用 final 修饰了,那么在它的代理类中,就无法重写该方法,而添加事务功能。

注意:如果某个方法是 static 的,同样无法通过动态代理,变成事务方法。

3.方法内部调用

有时候我们需要在某个 Service 类的某个方法中,调用另外一个事务方法,比如:


   
   
  1. @Service
  2. public class UserService {
  3. @Autowired
  4. private UserMapper userMapper;
  5. public void add (UserModel userModel) {
  6. userMapper.insertUser(userModel);
  7. updateStatus(userModel);
  8. }
  9. @Transactional
  10. public void updateStatus (UserModel userModel) {
  11. doSameThing();
  12. }
  13. }

我们看到在事务方法 add 中,直接调用事务方法 updateStatus。从前面介绍的内容可以知道,updateStatus 方法拥有事务的能力是因为 spring aop 生成代理了对象,但是这种方法直接调用了 this 对象的方法,所以 updateStatus 方法不会生成事务。

由此可见,在同一个类中的方法直接内部调用,会导致事务失效。

那么问题来了,如果有些场景,确实想在同一个类的某个方法中,调用它自己的另外一个方法,该怎么办呢?

3.1 新加一个 Service 方法

这个方法非常简单,只需要新加一个 Service 方法,把 @Transactional 注解加到新 Service 方法上,把需要事务执行的代码移到新方法中。具体代码如下:


   
   
  1. @Servcie
  2. public class ServiceA {
  3. @Autowired
  4. prvate ServiceB serviceB;
  5. public void save (User user) {
  6. queryData1();
  7. queryData2();
  8. serviceB.doSave(user);
  9. }
  10. }
  11. @Servcie
  12. public class ServiceB {
  13. @Transactional(rollbackFor=Exception.class)
  14. public void doSave (User user) {
  15. addData1();
  16. updateData2();
  17. }
  18. }

3.2 在该 Service 类中注入自己

如果不想再新加一个 Service 类,在该 Service 类中注入自己也是一种选择。具体代码如下:


   
   
  1. @Servcie
  2. public class ServiceA {
  3. @Autowired
  4. prvate ServiceA serviceA;
  5. public void save (User user) {
  6. queryData1();
  7. queryData2();
  8. serviceA.doSave(user);
  9. }
  10. @Transactional(rollbackFor=Exception.class)
  11. public void doSave (User user) {
  12. addData1();
  13. updateData2();
  14. }
  15. }

可能有些人会有这样的疑问:这种做法会不会出现循环依赖问题?

答案:不会。

其实 spring ioc 内部的三级缓存保证了它不会出现循环依赖问题。

3.3 通过 AopContent 类

在该 Service 类中使用 AopContext.currentProxy() 获取代理对象。

上面的方法 2 确实可以解决问题,但是代码看起来并不直观,还可以通过在该 Service 类中使用 AOPProxy 获取代理对象,实现相同的功能。具体代码如下:


   
   
  1. @Servcie
  2. public class ServiceA {
  3. public void save (User user) {
  4. queryData1();
  5. queryData2();
  6. ((ServiceA)AopContext.currentProxy()).doSave(user);
  7. }
  8. @Transactional(rollbackFor=Exception.class)
  9. public void doSave (User user) {
  10. addData1();
  11. updateData2();
  12. }
  13. }

 4.未被 spring 管理

在我们平时开发过程中,有个细节很容易被忽略,即使用 spring 事务的前提是:对象要被 spring 管理,需要创建 bean 实例。

通常情况下,我们通过 @Controller、@Service、@Component、@Repository 等注解,可以自动实现 bean 实例化和依赖注入的功能。

如果有一天,你匆匆忙忙地开发了一个 Service 类,但忘了加 @Service 注解,比如:


   
   
  1. //@Service
  2. public class UserService {
  3. @Transactional
  4. public void add (UserModel userModel) {
  5. saveData(userModel);
  6. updateData(userModel);
  7. }
  8. }

从上面的例子,我们可以看到 UserService 类没有加@Service注解,那么该类不会交给 spring 管理,所以它的 add 方法也不会生成事务。

5.多线程调用

在实际项目开发中,多线程的使用场景还是挺多的。如果 spring 事务用在多线程场景中,会有问题吗?


   
   
  1. @Slf4j
  2. @Service
  3. public class UserService {
  4. @Autowired
  5. private UserMapper userMapper;
  6. @Autowired
  7. private RoleService roleService;
  8. @Transactional
  9. public void add (UserModel userModel) throws Exception {
  10. userMapper.insertUser(userModel);
  11. new Thread(() -> {
  12. roleService.doOtherThing();
  13. }).start();
  14. }
  15. }
  16. @Service
  17. public class RoleService {
  18. @Transactional
  19. public void doOtherThing () {
  20. System.out.println( "保存role表数据");
  21. }
  22. }

从上面的例子中,我们可以看到事务方法 add 中,调用了事务方法 doOtherThing,但是事务方法 doOtherThing 是在另外一个线程中调用的。

这样会导致两个方法不在同一个线程中,获取到的数据库连接不一样,从而是两个不同的事务。如果想 doOtherThing 方法中抛了异常,add 方法也回滚是不可能的。

如果看过 spring 事务源码的朋友,可能会知道 spring 的事务是通过数据库连接来实现的。当前线程中保存了一个 map,key 是数据源,value 是数据库连接。


   
   
  1. private static final ThreadLocal<Map<Object, Object>> resources =
  2. new NamedThreadLocal<>( "Transactional resources");

我们说的同一个事务,其实是指同一个数据库连接,只有拥有同一个数据库连接才能同时提交和回滚。如果在不同的线程,拿到的数据库连接肯定是不一样的,所以是不同的事务。

6.表不支持事务

众所周知,在 mysql5 之前,默认的数据库引擎是myisam

它的好处就不用多说了:索引文件和数据文件是分开存储的,对于查多写少的单表操作,性能比 innodb 更好。

有些老项目中,可能还在用它。

在创建表的时候,只需要把ENGINE参数设置成MyISAM即可:


   
   
  1. CREATE TABLE `category` (
  2. `id` bigint NOT NULL AUTO_INCREMENT,
  3. `one_category` varchar( 20) COLLATE utf8mb4_bin DEFAULT NULL,
  4. `two_category` varchar( 20) COLLATE utf8mb4_bin DEFAULT NULL,
  5. `three_category` varchar( 20) COLLATE utf8mb4_bin DEFAULT NULL,
  6. `four_category` varchar( 20) COLLATE utf8mb4_bin DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE =MyISAM AUTO_INCREMENT = 4 DEFAULT CHARSET =utf8mb4 COLLATE =utf8mb4_bin

myisam 好用,但有个很致命的问题是:不支持事务

如果只是单表操作还好,不会出现太大的问题。但如果需要跨多张表操作,由于其不支持事务,数据极有可能会出现不完整的情况。

此外,myisam 还不支持行锁和外键。

所以在实际业务场景中,myisam 使用的并不多。在 mysql5 以后,myisam 已经逐渐退出了历史的舞台,取而代之的是 innodb。

有时候我们在开发的过程中,发现某张表的事务一直都没有生效,那不一定是 spring 事务的锅,最好确认一下你使用的那张表,是否支持事务。 

 7.未开启事务

有时候,事务没有生效的根本原因是没有开启事务。

你看到这句话可能会觉得好笑。

开启事务不是一个项目中,最最最基本的功能吗?

为什么还会没有开启事务?

没错,如果项目已经搭建好了,事务功能肯定是有的。

但如果你是在搭建项目 demo 的时候,只有一张表,而这张表的事务没有生效。那么会是什么原因造成的呢?

当然原因有很多,但没有开启事务,这个原因极其容易被忽略。

如果你使用的是 springboot 项目,那么你很幸运。因为 springboot 通过DataSourceTransactionManagerAutoConfiguration类,已经默默地帮你开启了事务。

你所要做的事情很简单,只需要配置spring.datasource相关参数即可。

但如果你使用的还是传统的 spring 项目,则需要在 applicationContext.xml 文件中,手动配置事务相关参数。如果忘了配置,事务肯定是不会生效的。

具体配置信息如下:


   
   
  1. <!-- 配置事务管理器 -->
  2. <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
  3. <property name="dataSource" ref="dataSource"> </property>
  4. </bean>
  5. <tx:advice id="advice" transaction-manager="transactionManager">
  6. <tx:attributes>
  7. <tx:method name="*" propagation="REQUIRED"/>
  8. </tx:attributes>
  9. </tx:advice>
  10. <!-- 用切点把事务切进去 -->
  11. <aop:config>
  12. <aop:pointcut expression="execution(* com.susan.*.*(..))" id="pointcut"/>
  13. <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
  14. </aop:config>

 默默地说一句,如果在 pointcut 标签中的切入点匹配规则,配错了的话,有些类的事务也不会生效。

二、事务不回滚

1.错误的传播特性 

其实,我们在使用@Transactional注解时,是可以指定propagation参数的。

该参数的作用是指定事务的传播特性,spring 目前支持 7 种传播特性:

  • REQUIRED 如果当前上下文中存在事务,则加入该事务,如果不存在事务,则创建一个事务,这是默认的传播属性值。

  • SUPPORTS 如果当前上下文中存在事务,则支持事务加入事务,如果不存在事务,则使用非事务的方式执行。

  • MANDATORY 当前上下文中必须存在事务,否则抛出异常。

  • REQUIRES_NEW 每次都会新建一个事务,并且同时将上下文中的事务挂起,执行当前新建事务完成以后,上下文事务恢复再执行。

  • NOT_SUPPORTED 如果当前上下文中存在事务,则挂起当前事务,然后新的方法在没有事务的环境中执行。

  • NEVER 如果当前上下文中存在事务,则抛出异常,否则在无事务环境上执行代码。

  • NESTED 如果当前上下文中存在事务,则嵌套事务执行,如果不存在事务,则新建事务。

如果我们在手动设置 propagation 参数的时候,把传播特性设置错了,比如:


   
   
  1. @Service
  2. public class UserService {
  3. @Transactional(propagation = Propagation.NEVER)
  4. public void add (UserModel userModel) {
  5. saveData(userModel);
  6. updateData(userModel);
  7. }
  8. }

 

我们可以看到 add 方法的事务传播特性定义成了 Propagation.NEVER,这种类型的传播特性不支持事务,如果有事务则会抛异常。

目前只有这三种传播特性才会创建新事务:REQUIRED,REQUIRES_NEW,NESTED。

2.自己吞了异常

事务不会回滚,最常见的问题是:开发者在代码中手动 try...catch 了异常。比如:


   
   
  1. @Slf4j
  2. @Service
  3. public class UserService {
  4. @Transactional
  5. public void add (UserModel userModel) {
  6. try {
  7. saveData(userModel);
  8. updateData(userModel);
  9. } catch (Exception e) {
  10. log.error(e.getMessage(), e);
  11. }
  12. }
  13. }

这种情况下 spring 事务当然不会回滚,因为开发者自己捕获了异常,又没有手动抛出,换句话说就是把异常吞掉了。

如果想要 spring 事务能够正常回滚,必须抛出它能够处理的异常。如果没有抛异常,则 spring 认为程序是正常的。

3.手动抛了别的异常

即使开发者没有手动捕获异常,但如果抛的异常不正确,spring 事务也不会回滚。


   
   
  1. @Slf4j
  2. @Service
  3. public class UserService {
  4. @Transactional
  5. public void add (UserModel userModel) throws Exception {
  6. try {
  7. saveData(userModel);
  8. updateData(userModel);
  9. } catch (Exception e) {
  10. log.error(e.getMessage(), e);
  11. throw new Exception(e);
  12. }
  13. }
  14. }

 

上面的这种情况,开发人员自己捕获了异常,又手动抛出了异常:Exception,事务同样不会回滚。

因为 spring 事务,默认情况下只会回滚RuntimeException(运行时异常)和Error(错误),对于普通的 Exception(非运行时异常),它不会回滚。

4.自定义了回滚异常

在使用 @Transactional 注解声明事务时,有时我们想自定义回滚的异常,spring 也是支持的。可以通过设置rollbackFor参数,来完成这个功能。

但如果这个参数的值设置错了,就会引出一些莫名其妙的问题,例如:


   
   
  1. @Slf4j
  2. @Service
  3. public class UserService {
  4. @Transactional(rollbackFor = BusinessException.class)
  5. public void add (UserModel userModel) throws Exception {
  6. saveData(userModel);
  7. updateData(userModel);
  8. }
  9. }

如果在执行上面这段代码,保存和更新数据时,程序报错了,抛了 SqlException、DuplicateKeyException 等异常。而 BusinessException 是我们自定义的异常,报错的异常不属于 BusinessException,所以事务也不会回滚。

即使 rollbackFor 有默认值,但阿里巴巴开发者规范中,还是要求开发者重新指定该参数。

这是为什么呢?

因为如果使用默认值,一旦程序抛出了 Exception,事务不会回滚,这会出现很大的 bug。所以,建议一般情况下,将该参数设置成:Exception 或 Throwable。

5.嵌套事务回滚多了


   
   
  1. public class UserService {
  2. @Autowired
  3. private UserMapper userMapper;
  4. @Autowired
  5. private RoleService roleService;
  6. @Transactional
  7. public void add (UserModel userModel) throws Exception {
  8. userMapper.insertUser(userModel);
  9. roleService.doOtherThing();
  10. }
  11. }
  12. @Service
  13. public class RoleService {
  14. @Transactional(propagation = Propagation.NESTED)
  15. public void doOtherThing () {
  16. System.out.println( "保存role表数据");
  17. }
  18. }

这种情况使用了嵌套的内部事务,原本是希望调用 roleService.doOtherThing 方法时,如果出现了异常,只回滚 doOtherThing 方法里的内容,不回滚  userMapper.insertUser 里的内容,即回滚保存点。但事实是,insertUser 也回滚了。

why?

因为 doOtherThing 方法出现了异常,没有手动捕获,会继续往上抛,到外层 add 方法的代理方法中捕获了异常。所以,这种情况是直接回滚了整个事务,不只回滚单个保存点。

怎么样才能只回滚保存点呢?


   
   
  1. @Slf4j
  2. @Service
  3. public class UserService {
  4. @Autowired
  5. private UserMapper userMapper;
  6. @Autowired
  7. private RoleService roleService;
  8. @Transactional
  9. public void add (UserModel userModel) throws Exception {
  10. userMapper.insertUser(userModel);
  11. try {
  12. roleService.doOtherThing();
  13. } catch (Exception e) {
  14. log.error(e.getMessage(), e);
  15. }
  16. }
  17. }

可以将内部嵌套事务放在 try/catch 中,并且不继续往上抛异常。这样就能保证,如果内部嵌套事务中出现异常,只回滚内部事务,而不影响外部事务。

三、其他

1 大事务问题

在使用 spring 事务时,有个让人非常头疼的问题,就是大事务问题。

通常情况下,我们会在方法上加@Transactional注解,添加事务功能,比如:


   
   
  1. @Service
  2. public class UserService {
  3. @Autowired
  4. private RoleService roleService;
  5. @Transactional
  6. public void add (UserModel userModel) throws Exception {
  7. query1();
  8. query2();
  9. query3();
  10. roleService.save(userModel);
  11. update(userModel);
  12. }
  13. }
  14. @Service
  15. public class RoleService {
  16. @Autowired
  17. private RoleService roleService;
  18. @Transactional
  19. public void save (UserModel userModel) throws Exception {
  20. query4();
  21. query5();
  22. query6();
  23. saveData(userModel);
  24. }
  25. }

@Transactional注解,如果被加到方法上,有个缺点就是整个方法都包含在事务当中了。

上面的这个例子中,在 UserService 类中,其实只有这两行才需要事务:


   
   
  1. roleService.save(userModel);
  2. update(userModel);

在 RoleService 类中,只有这一行需要事务:

saveData(userModel);
   
   

现在的这种写法,会导致所有的 query 方法也被包含在同一个事务当中。

如果 query 方法非常多,调用层级很深,而且有部分查询方法比较耗时的话,会造成整个事务非常耗时,而从造成大事务问题。

2.编程式事务

上面聊的这些内容都是基于@Transactional注解的,主要说的是它的事务问题,我们把这种事务叫做:声明式事务

其实,spring 还提供了另外一种创建事务的方式,即通过手动编写代码实现的事务,我们把这种事务叫做:编程式事务。例如:


   
   
  1. @Autowired
  2. private TransactionTemplate transactionTemplate;
  3. ...
  4. public void save (final User user) {
  5. queryData1();
  6. queryData2();
  7. transactionTemplate.execute((status) => {
  8. addData1();
  9. updateData2();
  10. return Boolean.TRUE;
  11. })
  12. }

在 spring 中为了支持编程式事务,专门提供了一个类:TransactionTemplate,在它的 execute 方法中,就实现了事务的功能。

相较于@Transactional注解声明式事务,我更建议大家使用基于TransactionTemplate的编程式事务。主要原因如下:

  1. 避免由于 spring aop 问题导致事务失效的问题。

  2. 能够更小粒度地控制事务的范围,更直观。

建议在项目中少使用 @Transactional 注解开启事务。但并不是说一定不能用它,如果项目中有些业务逻辑比较简单,而且不经常变动,使用 @Transactional 注解开启事务也无妨,因为它更简单,开发效率更高,但是千万要小心事务失效的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值