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

来源:互联网 发布:电话属于网络吗 编辑:程序博客网 时间:2024/06/03 17:39

1:在同一个service中嵌套, 如果已经存在外层事务,则nested不会开启新的事务,否则会开启
nested的savepoint是不起作用的, 内层事务回滚会导致整个事务一同回滚
2:在不同的service中嵌套,如果已经存在外层事务,则nested同样不会开启新的事务,否则会开启
但是nested的savepoint是起作用的,即:内层事务回滚 只会影响内层事务,不会导致外层事务一同回滚

先来测试第一种情况

@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);            this.update(userParam);//          TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 只回滚到内层事务的savepoint        } catch (InterruptedException e) {            e.printStackTrace();        } catch (Exception ae) {            ae.printStackTrace();        }    }    @Transactional(propagation = Propagation.NESTED)    @Override    public Integer update(UserRecord userParam) {        logger.info("开始执行 update {}, {}", userParam.getId(), userParam.getPhone());        // userParam.setId(28);        // userParam.setPhone("15200001234");        userParam.setName("coffee");        int result = userMapper.update(userParam);        try {            logger.info("update 完成---数据库中的值为 {} 开始 sleep");            Thread.sleep(1000 * 10);            logger.info("sleep结束");            // throw new RuntimeException(); //外层事务也回滚            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//只回滚到内层事务的savepoint        } catch (InterruptedException e) {            e.printStackTrace();        }        return result;    }
2017-12-24 11:18:19 [http-nio-9082-exec-1] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 save null, 152107123452017-12-24 11:18:19 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession2017-12-24 11:18:19 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@e48d997]2017-12-24 11:18:19 [http-nio-9082-exec-1] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@4528e9b7] will be managed by Spring2017-12-24 11:18:19 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@e48d997]2017-12-24 11:18:19 [http-nio-9082-exec-1] INFO  c.c.service.impl.UserServiceImpl2 - save 完成---数据库中的值为 2226392017-12-24 11:18:20 [http-nio-9082-exec-1] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 update 222639, xxx2017-12-24 11:18:22 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@e48d997] from current transaction2017-12-24T11:18:22.883+0800: [GC (Allocation Failure) [PSYoungGen: 28333K->1306K(30720K)] 79340K->52321K(99328K), 0.0054421 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 2017-12-24 11:18:23 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@e48d997]2017-12-24 11:18:23 [http-nio-9082-exec-1] INFO  c.c.service.impl.UserServiceImpl2 - update 完成---数据库中的值为 {} 开始 sleep2017-12-24 11:18:33 [http-nio-9082-exec-1] INFO  c.c.service.impl.UserServiceImpl2 - sleep结束2017-12-24 11:18:33 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@e48d997]2017-12-24 11:18:33 [http-nio-9082-exec-1] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@e48d997]

接下来测试第二种情况

@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);            // this.update(userParam);            // TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 只回滚到内层事务的savepoint        } catch (InterruptedException e) {            e.printStackTrace();        } catch (Exception ae) {            ae.printStackTrace();        }    }// service3中的save方法@Transactional(propagation = Propagation.NESTED)    @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();            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//只回滚到内层事务的savepoint        } catch (InterruptedException e) {            e.printStackTrace();        }    }

通过日志发现 同样不会开启新的事务,但是回滚的时候 内层事务 不会影响到外层事务

2017-12-24 11:29:55 [http-nio-9082-exec-9] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 save null, 152107123452017-12-24 11:29:55 [http-nio-9082-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession2017-12-24 11:29:55 [http-nio-9082-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1372e7ef]2017-12-24 11:29:55 [http-nio-9082-exec-9] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@6fd3f600] will be managed by Spring2017-12-24 11:29:55 [http-nio-9082-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1372e7ef]2017-12-24 11:29:55 [http-nio-9082-exec-9] INFO  c.c.service.impl.UserServiceImpl2 - save 完成---数据库中的值为 2226422017-12-24 11:29:56 [http-nio-9082-exec-9] INFO  c.c.service.impl.UserServiceImpl3 - 开始执行 save 222642, xxx2017-12-24 11:29:56 [http-nio-9082-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1372e7ef] from current transaction2017-12-24 11:29:56 [http-nio-9082-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1372e7ef]2017-12-24 11:29:56 [http-nio-9082-exec-9] INFO  c.c.service.impl.UserServiceImpl3 - save 完成---数据库中的值为 222643 开始 sleep2017-12-24 11:29:57 [http-nio-9082-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1372e7ef]2017-12-24 11:29:57 [http-nio-9082-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1372e7ef]2017-12-24 11:29:57 [http-nio-9082-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1372e7ef]