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
  • 相关阅读:
    MP3文件格式解析
    各种流媒体服务器的架设(一)
    fread函数和fwrite函数
    [转]C#算法 一对小兔子一年后长成大兔子;一对大兔子每半年生一对小兔子。大兔子的繁殖期为4年,兔子的寿命是6年。假定第一年年初投放了一对小兔子,试编程计算,第n年末总共会有多少对兔子
    C#算法 质因数 最大公约数与最小公倍数
    数据库删除语句 Drop/Delete/Truncate比较
    [转]C# 截取指定长度的中英文混合字符串的算法
    C#算法 母牛从第4年起每年生一头小母牛,并且母牛不会死
    C#算法 有一个母羊,第2年和第4年可以生一头小母羊,在第5年死去,小母羊在它出生的第2年和第4年生小母羊,第5年死去
    C#算法 最值/平均
  • 原文地址:https://www.cnblogs.com/fanBlog/p/13652583.html
Copyright © 2011-2022 走看看