zoukankan      html  css  js  c++  java
  • Spring事务的传播Propagation

    事务传播方式:

    1.REQUIRES_NEW

    /***
         * @description 方法加事务  REQUIRES_NEW ,内部方法也加事务 REQUIRES_NEW 以哪个事务为准
         * @methodName testTrancNew 
         * @date 2020/9/10 20:02
         * @param
         * @return void
         * @throws
         */
        @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
        @Override
        public void testTrancNew() {
    
            /**
             * begin tran A
             * begin tran B
             * cityDataService.trancNewARequires_new() 事务传播方式 REQUIRES_NEW 不受外部方法的影响,内部买没有异常就提交成功
             * commit B
             * insert()
             * trancNewBThrowException()
             * rollback A
             */
            //不会回滚
            cityDataService.trancNewARequires_new();
    
            Mycity city = new Mycity();
            city.setCountry("外部方法 REQUIRES_NEW");
            city.setName("中国A");
    
            mycityMapper.insert(city);
            //抛异常
            cityDataService.trancNewBThrowException();
     
        }
    //cityDataService:
        @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
        @Override
        public void trancNewARequires_new() {
            City city = new City();
            city.setCountry("内部方法 REQUIRES_NEW");
            city.setName("中国A");
            cityMapper.insert(city);
        }
    
    
     @Override
        public void trancNewBThrowException() {
            City city = new City();
            city.setCountry("methodA");
            city.setName("中国A");
            int a = 4;
            int cc = a / 0;
            cityMapper.insert(city);
        }
    
    REQUIRES_NEW
    REQUIRES_NEW

     2.REQUIRED

      @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
        @Override
        public void testTrancRequired() {
    
    //        如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
    //        (有C里面调用 方法A,方法B 方法B有异常 方法A也回滚)
            /**
             * begin tranc
             * cityDataService.trancNewARequired() 事务方式REQUIRED
             * cityDataService.updateTrancBRequired();事务方式REQUIRED
             * mycityMapper.insert(city);
             * trancNewBThrowException 异常
             * rollback
             */
    
            cityDataService.trancNewARequired();
    
            cityDataService.updateTrancBRequired();
            Mycity city = new Mycity();
            city.setCountry("methodA REQUIRED insert");
            city.setName("中国A");
            mycityMapper.insert(city);
            //抛异常
            cityDataService.trancNewBThrowException();
    
        }
    //cityDataService
    
        @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
        @Override
        public void trancNewARequired() {
            City city = new City();
            city.setCountry("methodB REQUIRED insert");
            city.setName("中国A");
            cityMapper.insert(city);
        }
       @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
        @Override
        public void updateTrancBRequired() {
            City city = new City();
            city.setCountry("methodB REQUIRED 修改成功");
            city.setName("中国A");
            city.setId(3L);
            cityMapper.updateById(city);
        }
        @Override
        public void trancNewBThrowException() {
            City city = new City();
            city.setCountry("methodA");
            city.setName("中国A");
            int a = 4;
            int cc = a / 0;
            cityMapper.insert(city);
        }
    REQUIRED

    3.REQUIRED and  REQUIRES_NEW 组合

        @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
        @Override
        public void testTrancRequiredAndRequNes() {
     
            /**
             * begin tran A
             * begin tran B
             * cityDataService.trancNewARequires_new() 事务方式 Propagation.REQUIRES_NEW 不回滚
             * commit B
             * cityDataService.updateTrancBRequired()  事务方式 Propagation.REQUIRED 回滚
             * mycityMapper.insert(city); //当前方法 回滚
             * cityDataService.trancNewBThrowException 抛出异常
             * rollback A
             */
    
            cityDataService.trancNewARequires_new();
    
            cityDataService.updateTrancBRequired();
            Mycity city = new Mycity();
            city.setCountry("methodA REQUIRED insert");
            city.setName("中国A");
            mycityMapper.insert(city);
            //抛异常
            cityDataService.trancNewBThrowException();
    
        }
    
        @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
        @Override
        public void trancNewARequires_new() {
            City city = new City();
            city.setCountry("内部方法 REQUIRES_NEW");
            city.setName("中国A");
            cityMapper.insert(city);
        }
    
        @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
        @Override
        public void updateTrancBRequired() {
            City city = new City();
            city.setCountry("methodB REQUIRED 修改成功");
            city.setName("中国A");
            city.setId(3L);
            cityMapper.updateById(city);
        }
    REQUIRED and REQUIRES_NEW

    4.SUPPORTS and SUPPORTS

      @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
        @Override
        public void testTrancSupports() {
            /**
             * 全部执行成功 非事务方式
             */
    
            cityDataService.trancNewASupports();
    
            Mycity city = new Mycity();
            city.setCountry("methodA");
            city.setName("中国A");
            mycityMapper.insert(city);
            //抛异常
            cityDataService.trancNewBThrowException();
    
        }
        @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
        @Override
        public void trancNewASupports() {
            City city = new City();
            city.setCountry("methodA");
            city.setName("中国A");
            cityMapper.insert(city);
        }
    SUPPORTS and SUPPORTS

    5. REQUIRES_NEW and  SUPPORTS

     @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
        @Override
        public void testTrancNewAndSupport() {
            /**
             *  begin tran A
             *  cityDataService.trancNewASupports() 事务方式 SUPPORTS 回滚
             *  mycityMapper.insert(city);
             *  cityDataService.trancNewBThrowException()
             *  rollback A
             */
    
            cityDataService.trancNewASupports();
    
            Mycity city = new Mycity();
            city.setCountry("methodA");
            city.setName("中国A");
            mycityMapper.insert(city);
            //抛异常
            cityDataService.trancNewBThrowException();
    
        }
     @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
        @Override
        public void trancNewASupports() {
            City city = new City();
            city.setCountry("methodA");
            city.setName("中国A");
            cityMapper.insert(city);
        }
    REQUIRES_NEW and SUPPORTS
  • 相关阅读:
    Build MySQL 5.7.4 in RedHat
    glibc-2.19 之 strlen 实现
    [部署问题列表]
    [数据链路排查]
    VS配置本地IIS以域名访问
    SQL系列函数——数学函数
    面向对象编程的几个原则
    SQL系列函数——字符串函数
    SQL数据类型
    DHtmlx组件获取选中行的某一列对应的值
  • 原文地址:https://www.cnblogs.com/fanBlog/p/13652583.html
Copyright © 2011-2022 走看看