zoukankan      html  css  js  c++  java
  • spring 局部事务

    spring 支持注解式事务,作用于方法级别,我们如果想将事务放进行级别,可以使用编程式事务,如使用 TransactionTemplate。还有另外一种使用 Lambda 表达式,结合事务注解来达到行级别的事务,通过将一个方法接口传入,在一个注解事务方法中执行,就达到了行级别的事务控制。

    public class Transactions {
    
        private static final ThreadLocal<Boolean> tl = new ThreadLocal<>();
    
        @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class, timeout = 15)
        public <T> T requiredNew(Supplier<T> supplier) {
            Boolean flag = tl.get();
            if (flag != null) {
                throw new RuntimeException("nested transaction is not supported!");
            }
            try {
                tl.set(true);
                return supplier.get();
            } finally {
                tl.remove();
            }
        }
    
        @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class, timeout = 15)
        public <T> T required(Supplier<T> supplier) {
            return supplier.get();
        }
    }
    
    @Service
    public class Dmeo{
      @Autowired
      private Transactions transactions;
    
      public void demo(){
          transactions.required(() -> {
          //  do something
          });
      }
    }
    
  • 相关阅读:
    vue-cli与后台数据交互增删改查
    echart地图下钻
    Vue中data重置问题
    页面滚动tab监听
    less笔记
    bootstrap-table 行内编辑
    bootstrap-table固定表头固定列
    微信分享配置(js-sdk)
    npm查看全局安装过的包
    页面固定定位超出一屏
  • 原文地址:https://www.cnblogs.com/diandiandidi/p/15741635.html
Copyright © 2011-2022 走看看