• Java - @Transaction 异常不回滚



    今天又遇到了 @Transaction 注解失效的情况,明明方法已经发生了运行时错误,但是数据并没有回滚,所以在这里记录一下可能导致注解失效的几种情况。

    @Transaction 核心点

    事务的实现其实是Spring 通过默认动态代理的方式实现 AOP, 从而实现对目标方法的增强 ,即对带有 @Transaction 注解的方法实现事务相关逻辑的增强,我们真正调用到能够实现事务的方法实质上是调用代理过的对象中被增强过的方法。
    显然有重要的两点:

    1. Spring 管理
    2. 动态代理

    事务不回滚的情况

    没有抛出 RuntimeException 或 Error

    只有代码抛出 RuntimeException 运行时异常或 Error的时候才会触发事务回滚。

    异常被捕获

    注意抛出异常的代码是否被 try…catch… 包裹,是否在获取到异常之后抛出了运行时异常。
    如果未抛出,则事务回滚不生效,可以手动抛出异常,也可以手动回滚。
    如下,catch 中的两种的方法均可触发回滚效果:

        @Transactional
        public void transTerst() {
            try {
                testDao.insertTest("1");
                testDao.insertTest("2");
                System.out.println(10/0);
            }catch (Exception ex){
    //            throw new RuntimeException(ex.getMessage());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    @Transaction 未定义在public方法上

    因为 Spring 通过动态代理实现 AOP 对方法进行增强而实现事务的一系列相关逻辑,所以从根本之上,Spring 需要能获取到该方法的代理,而 Spring 无法代理到 private 方法上。

    @Transaction 方法未被代理增强

    没使用被增强过的方法

    如下代码,回滚并不会生效:

    @Service
    public class TransServiceImpl implements ITransService {
    
        @Autowired
        TestDao testDao;
    
        @Transactional
        public void transTest1() {
            testDao.insertTest("3");
            testDao.insertTest("4");
        }
    
        @Override
        public void transTest() {
            try {
                transTest1();
                this.transTest1();
                System.out.println(10/0);
            }catch (Exception ex){
                throw new RuntimeException(ex.getMessage());
            }
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    重点在与这个方法所在的位置。
    其实,在 transTest 方法中 的 transTest1();this.transTest1(); 的意义是一样的,transTest1() 是当前这个类中的方法,this 代表的是这个类自己,这两种写法都是在当前类找到 transTest1() 方法并调用。
    而当前类肯定是不会做多余的工作去代理一个自己出来,所以我们在调用 transTest1() 的时候,使用的其实是该类中自身的方法,而不是通过 Spring 增强过的方法,自然没有事务的相关性质。
    所以我们需要调用被 Spring 增强过的 transTest1 方法,改成如下代码:

    @Service
    public class TransServiceImpl implements ITransService {
    
        @Autowired
        TestDao testDao;
    
        @Autowired
        ITransService transService;
        
        @Transactional
        public void transTest1() {...}
    
        @Override
        public void transTest() {
            try {
                transService.transTest1();
                transService.transTest1();
                System.out.println(10/0);
            }catch (Exception ex){
                throw new RuntimeException(ex.getMessage());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    类未被代理

    还有一点也需要注意:

    @Service
    public class TransServiceImpl implements ITransService {
    ...
    }
    
    • 1
    • 2
    • 3
    • 4

    这个类中的 @Service 注解也是很关键的,他和 @Component@Configuration 注解一样,代表这个类被注册为 Spring 容器中的 Bean,受 Spring 容器管理。

    事务的传播性质

    有的时候我们并不是简单的想回滚所有的操作,比如我们有一个主方法里面我们操作了表A ,在我们的子方法里面,我们操作了表B,我们需要的情景是,表B出现的异常导致的数据回滚不影响表A的插入。
    首先我们尝试使用 try…catch… 获取主方法中的异常,不让其抛出运行时异常。

    @Mapper
    public interface TestDao extends BaseMapper<Object> {
        @Insert({"insert into a (field_a)values(#{testValue})"})
        long insertA(String testValue);
    
        @Insert({"insert into b (field_b)values(#{testValue})"})
        long insertB(String testValue);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    @Service
    public class TransServiceImpl implements ITransService {
    
        @Autowired
        TestDao testDao;
    
        @Autowired
        ITransService transService;
    
        @Transactional
        public void insertB() {
            testDao.insertB("b");
            System.out.println(10/0);
        }
    
        @Override
        @Transactional
        public void transTest() {
            try {
                testDao.insertA("a");
                transService.insertB();
            }catch (Exception ex){
    //            DO NOTHING
    //            throw new RuntimeException(ex.getMessage());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    没有异常抛出但是事务回滚

    在我们的预期效果中,表B回滚,表A数据正常保存,但是事实上并不是这样。
    两个表里面都没有数据,即 transTest 方法中的 SQL 操作也回滚了。
    在这里插入图片描述
    在这里插入图片描述
    主方法明明没有抛出异常却发生了事务回滚,其实是因为事物的传播性质
    子方法沿用了主方法的事务,即插入表A和插入表B这两个操作其实是在同一个事务中进行的。
    子方法中发生异常导致事务回滚,主方法提交事务时发现当前事务已经被标记为回滚,无法提交事务,只能执行静默回滚操作
    解决办法也很简单,只要主方法和子方法不在一个事务里面就可以了,使用 @Transaction 注解中的 propagation 属性为子方法单独创建一个事务,修改如下:

    @Service
    public class TransServiceImpl implements ITransService {
        @Autowired
        TestDao testDao;
    
        @Autowired
        ITransService transService;
    
        @Transactional(propagation = Propagation.REQUIRES_NEW)
        public void insertB() {...}
    
        @Override
        @Transactional
        public void transTerst() {...}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    这样子方法中异常就不会影响主方法中的插入了。

  • 相关阅读:
    华为HMS Core携手超图为三维GIS注入新动能
    FFmpeg抓取RTSP图像进行图像分析
    FestDFS
    Spring系列五:Spring怎么解决循环依赖
    金融强化学习与finRL开发包
    3.6研究代码(2)
    寻找文本之间的不同
    java计算机毕业设计基于springboo+vue的旅游自驾游攻略方案分享系统
    Linux内核源码中最常见的数据结构之【Spinlock】
    CSDN博客专家
  • 原文地址:https://blog.csdn.net/qq_40096897/article/details/126354560