mysql的事务传播机制的测试

public class Test1Service {
    @Autowired
    private ProductMapper productMapper;

    /**
     * 测试1
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public void save1(){
//        saveProduct("测试2","jhwdfhj");
        update();
//         throw  new RuntimeException("运行异常");
        log.info("更新完");

    }
    /**
     * 测试2
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void save2(){
        saveProduct("测试2","jhwdfhj");
//        update();
        log.info("更新完了");
        throw  new RuntimeException("运行异常");

    }
    /**
     * 测试3
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NESTED)
    public void save3(){
        saveProduct("测试2","jhwdfhj");
//        update();
        log.info("更新完了");
        throw  new RuntimeException("运行异常");

    }
    @Transactional(rollbackFor = Exception.class)
    public void save4(){
        saveProduct("测试2","jhwdfhj");
//        update();
        log.info("更新完了");
        throw  new RuntimeException("运行异常");

    }

    private  void update(){
        Product product = new Product();
        product.setName("更新2");
        product.setItemNumber("6666");
        product.setProductDetail("");
        product.setId(1104L);
        productMapper.updateByPrimaryKeySelective(product);
    }

    private void saveProduct(String name,String item) {
        Product product = new Product();
        product.setName(name);
        product.setItemNumber(item);
        product.setProductDetail("");
        productMapper.insertSelective(product);
    }

}
public class TransactionaTestService {
    @Autowired
    private Test1Service test1;
    @Autowired
    private Test2Service test2;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 测试  Propagation.NOT_SUPPORTED
     * 结果:
     * test1 以非事务的方式执行 不会回滚 主任务接收到异常 会回滚
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void save1(){
        insert("1313");
        try {
            test1.save1();
        } catch (Exception e) {
          log.error("test1 异常被捕获");
        }
    }

    /**
     * 测试 Propagation.REQUIRES_NEW
     * 测试结果:
     * test1 会创建一个独立的事务
     * 子事务回滚 父事务可以的通过trycatch 选择是否回滚
     * 父事务 发生异常回滚不会影响到子事务
     * 要注意 同步的情况
     * 父事务是在子事务提交后才能提交 如果 父事务如果更新了某条数据 恰好子事务也需要更新
     * 父事务等待子事务提交 子事务等待父事务释放锁 -> 死锁
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void save2(){
//        insert("25451");
        update();
        test1.save2();
        List<Product> products = productMapper.selectByExample(new ProductCriteria());
//        throw  new RuntimeException("运行异常");
    }

    /**
     * 测试:propagation = Propagation.NESTED
     *  内部事务回滚 外围事务可以不回滚(try catch)
     *  外部事务回滚 内部事务一定也回滚
     *  实际上 内部事务和外部事务 是同一个事务
     */
    @Transactional(rollbackFor = Exception.class)
    public void save3(){
        insert("11111");
        insert("2222");
            test1.save3();

    }

    /**
     * 测试:propagational = required
     * 测试结果:内部事务和外部事务都是同一个事务 有任何一方回滚 内部和外部事务都会回滚
     */
    @Transactional(rollbackFor = Exception.class)
    public  void save4(){
        insert("11111");
        try {
            test1.save4();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }








    private void insert(String s1) {
        Product product = new Product();
        product.setName(s1);
        product.setItemNumber(s1);
        product.setProductDetail(s1);
        productMapper.insertSelective(product);
    }

    public void update(){
        Product product = new Product();
        product.setName("更新1");
        product.setItemNumber("1544");
        product.setProductDetail("");
        product.setId(1103L);
        productMapper.updateByPrimaryKeySelective(product);
//        ProductCriteria criteria = new ProductCriteria();
//        criteria.createCriteria().andNameEqualTo("测试2");
//        productMapper.updateByExampleSelective(product,criteria);
    }
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页