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

直接上代码

    /**
     * <pre>
     * 分两种情况 1、事务不嵌套 每次执行方法save的时候都会开启一个新的事务 
     * 2、事务嵌套 外层事务如果开启,内层事务会加入到外层事务中,此时如果 外层事务回滚,则内层事务会一同回滚,
     * 如果内层事务回滚、外层事务也会一同回滚。。。。因为他俩就是同一个事务啊
     * </pre>
     */
    @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());
            // if ("15210712347".equals(userParam.getPhone())) {
            // throw new RuntimeException();
            // }
            Thread.sleep(1000);
            this.update(userParam);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * REQUIRES_NEW 如果已经存在老事务,则新事务会被挂起,直到老事务执行完成以后才执行新的事务<br/>
     * 注意: 并不是新事务把等着老事务把update方法的所有code都执行完才开始执行新事物的update方法<br/>
     * 因为只有执行到userMapper.update(userParam)才开启事务, 此时才会挂起老的事务
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Integer update(UserRecord userParam) {
        logger.info("开始执行 update {}, {}", userParam.getId(), userParam.getPhone());
        userParam.setName("coffee");
        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-23 22:54:29 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 save null, 15210712345
2017-12-23 22:54:29 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2017-12-23 22:54:29 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@634cbdcc]
2017-12-23 22:54:29 [http-nio-9082-exec-3] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@37afbb72] will be managed by Spring
2017-12-23 22:54:29 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@634cbdcc]
2017-12-23 22:54:29 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - save 完成---数据库中的值为 null 开始 sleep
2017-12-23 22:54:30 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - 开始执行 update null, 15210712345
2017-12-23 22:54:30 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@634cbdcc] from current transaction
2017-12-23 22:54:30 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@634cbdcc]
2017-12-23 22:54:30 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - update 完成---数据库中的值为 {} 开始 sleep
2017-12-23 22:54:40 [http-nio-9082-exec-3] INFO  c.c.service.impl.UserServiceImpl2 - sleep结束
2017-12-23 22:54:40 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@634cbdcc]
2017-12-23 22:54:40 [http-nio-9082-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@634cbdcc]
## 抛异常会导致回滚,  不管在外层事务 还是内层事务 都会导致整个事务失效(内外层事务 压根就是同一个)
十二月 23, 2017 10:54:40 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springServlet] in context with path [/coffee.controller] threw exception [Request processing failed; nested exception is java.lang.RuntimeException] with root cause
java.lang.RuntimeException
    at com.coffee.service.impl.UserServiceImpl2.update(UserServiceImpl2.java:71)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值