事务传播方式:
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
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); }
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); }
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); }
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); }