• 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
  • 相关阅读:
    IBM Lotus网站荟萃
    Lotus 深入浅出系列——前言(二)IBM Lotus开发人员的几个境界
    在IIS上配置和测试Perl脚本
    网站推广
    iTOP开发板MiniLinuxC程序调用shell命令
    iTOP4412开发板_驱动_adc驱动升级和测试例程
    最近想入手树莓派3来学习编程同学没有选择4412开发板吗?
    iTOP4412开发板串口转接小板的使用文档
    学习嵌入式4412开发板手把手配套视频_2000人群组在线交流
    电子医疗设备创新研发应该用i.MX6Q开发板吗?为医疗设备提供解决方案
  • 原文地址:https://www.cnblogs.com/fanBlog/p/13652583.html
Copyright © 2020-2023  润新知