spring事务传播机制-REQUIRED嵌套REQUIRES_NEW

如果在同一个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;
    }
2017-12-24 08:46:43 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 save null, 15210712345
2017-12-24 08:46:43 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2017-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 Spring
2017-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 开始 sleep
2017-12-24 08:46:44 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 update 222599, 15210712345
2017-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 transaction
2017-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 完成---数据库中的值为 {} 开始 sleep
2017-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]

测试两个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();
        }
    }
@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();
        }
    }

执行的日志如下

2017-12-24 10:59:43 [http-nio-9082-exec-6] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 save null, 15210712345
2017-12-24 10:59:43 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2017-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 Spring
2017-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 完成---数据库中的值为 222637
2017-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, xxx
2017-12-24 10:59:44 [http-nio-9082-exec-6] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2017-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 Spring
2017-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 开始 sleep
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@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]

你可能感兴趣的:(Spring)