首页 > 编程语言 > 详细

Spring事务的传播Propagation

时间:2020-09-11 18:49:36      阅读:69      评论:0      收藏:0      [点我收藏+]

 

事务传播方式:

1.REQUIRES_NEW

技术分享图片
/***
     * @description 方法加事务  REQUIRES_NEW ,内部方法也加事务 REQUIRES_NEW 以哪个事务为准
     * @methodName testTrancNew
     * @author yanp.fan001
     * @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

 

 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

 

Spring事务的传播Propagation

原文:https://www.cnblogs.com/fanBlog/p/13652583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!