spring事务传播机制-REQUIRED嵌套REQUIRES NEW

本文探讨了Spring事务管理中的REQUIRED和REQUIRES_NEW两种传播级别。当在同一个Service类内嵌套调用时,即使内层使用REQUIRES_NEW,也不会开启新事务,导致整体事务回滚。而在不同Service类之间调用,REQUIRES_NEW则能确保新事务独立,回滚互不影响。通过测试代码和日志分析,详细展示了这两种事务传播特性的实际应用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                       

如果在同一个service类中定义的两个方法, 内层REQUIRES_NEW并不会开启新的事务,save和update中回滚都会导致整个事务的回滚
如果在不同的service中定义的两个方法, 内层REQUIRES_NEW会开启新的事务,并且二者独立,事务回滚互不影响

测试:以下代码定义在UserService2中

/**    @Transactional(propagation = Propagation.REQUIRED)    @Override    public void save(UserRecord userParam) {        logger.info("开始执行 save {}, {}", userParam.getId(), userParam.getPhone());        userMapper.save(userParam);        try {            logger.info("save 完成---数据库中的值为 {} 开始 sleep", userParam.getId());            Thread.sleep(1000);            this.update(userParam);//          throw new RuntimeException();        } catch (InterruptedException e) {            e.printStackTrace();        }    }    @Transactional(propagation = Propagation.REQUIRES_NEW)    @Override    public Integer update(UserRecord userParam) {        logger.info("开始执行 update {}, {}", userParam.getId(), userParam.getPhone());        int result = userMapper.update(userParam);        try {            logger.info("update 完成---数据库中的值为 {} 开始 sleep");            Thread.sleep(1000 * 10);            logger.info("sleep结束");//          throw new RuntimeException();        } catch (InterruptedException e) {            e.printStackTrace();        }        return result;    }
  
  
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
2017-12-24 08:46:43 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 save null, 152107123452017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession2017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df]2017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@665e820d] will be managed by Spring2017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df]2017-12-24 08:46:43 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - save 完成---数据库中的值为 222599 开始 sleep2017-12-24 08:46:44 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 update 222599, 152107123452017-12-24 08:46:44 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df] from current transaction2017-12-24 08:46:44 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df]2017-12-24 08:46:44 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - update 完成---数据库中的值为 {} 开始 sleep2017-12-24 08:46:54 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - sleep结束2017-12-24 08:46:54 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df]2017-12-24 08:46:54 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df]2017-12-24 08:46:54 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65e040df]
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

测试两个save分别定义在UserService2和UserService3中

    @Transactional(propagation = Propagation.REQUIRED)    @Override    public void save(UserRecord userParam) {        logger.info("开始执行 save {}, {}", userParam.getId(), userParam.getPhone());        userMapper.save(userParam);        try {            logger.info("save 完成---数据库中的值为 {}", userParam.getId());            // if ("15210712347".equals(userParam.getPhone())) {            // throw new RuntimeException();            // }            Thread.sleep(1000);            userParam.setName("xxx");            userParam.setPhone("xxx");            userService3.save(userParam);//          throw new RuntimeException();        } catch (InterruptedException e) {            e.printStackTrace();        } catch (AppException ae) {            ae.printStackTrace();        }    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
@Transactional(propagation = Propagation.REQUIRES_NEW)    @Override    public void save(UserRecord userParam) {        logger.info("开始执行 save {}, {}", userParam.getId(), userParam.getPhone());        userMapper.save(userParam);        try {            logger.info("save 完成---数据库中的值为 {} 开始 sleep", userParam.getId());            Thread.sleep(1000);//          throw new RuntimeException();// 注意在这抛出异常 会导致异常继续向上传递到userService2.save中导致两个事务都回滚            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//只回滚到内层事务的,不会影响到userServive2的save回滚        } catch (InterruptedException e) {            e.printStackTrace();        }    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

执行的日志如下

2017-12-24 10:59:43 [http-nio-9082-exec-6] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 save null, 152107123452017-12-24 10:59:43 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession2017-12-24 10:59:43 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3321d64f]2017-12-24 10:59:43 [http-nio-9082-exec-6] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@4528e9b7] will be managed by Spring2017-12-24 10:59:43 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3321d64f]2017-12-24 10:59:43 [http-nio-9082-exec-6] INFO  c.c.service.impl.UserServiceImpl2 - save 完成---数据库中的值为 2226372017-12-24 10:59:44 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization suspending SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3321d64f]2017-12-24 10:59:44 [http-nio-9082-exec-6] INFO  c.c.service.impl.UserServiceImpl3 - 开始执行 save 222637, xxx2017-12-24 10:59:44 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession2017-12-24 10:59:44 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@53dd397f]2017-12-24 10:59:44 [http-nio-9082-exec-6] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@58a60a8c] will be managed by Spring2017-12-24 10:59:44 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@53dd397f]2017-12-24 10:59:44 [http-nio-9082-exec-6] INFO  c.c.service.impl.UserServiceImpl3 - save 完成---数据库中的值为 222638 开始 sleep2017-12-24 10:59:45 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@53dd397f]2017-12-24 10:59:45 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@53dd397f]2017-12-24 10:59:45 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization resuming SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3321d64f]2017-12-24 10:59:45 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3321d64f]2017-12-24 10:59:45 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3321d64f]2017-12-24 10:59:45 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3321d64f]
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值