spring事务的传播行为

spring事务的传播行为

spring事务的传播行为有7个:

  1. PROPAGATION_REQUIRED:支持当前事务,如果不存在就创建一个
  2. PROPAGATION_SUPPORTS:支持当前事务,如果不存在就不使用事务
  3. PROPAGATION_MANDATORY:支持当前事务,如果不存在就抛出异常
  4. PROPAGATION_REQUIRES_NEW:如果当前存在事务,就将当前事务挂起,新建一个事务
  5. PROPAGATION_NOT_SUPPORTED:以非事务的方式运行,如果当前存在事务,就将其挂起
  6. PROPAGATION_NEVER:以非事务的方式运行,如果存在事务,抛出异常
  7. PROPAGATION_NESTED:如果事务存在,则嵌套事务运行,若事务不存在则与PROPAGATION_REQUIRED的效果相同

数据库信息

在这里插入图片描述

PROPAGATION_REQUIRED 运行结果

  1. 两个方法都被事务修饰
// 测试类
@Test
    public void otherAllTransaction() throws MyException {
        User user = new User(10, "abcd", "1234567890");
        userService.otherAllTransaction(9, user);

    }
	/**
     * 如果当前存在事务,则加入当前事务
     * @param id
     * @param user
     * @throws MyException
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void otherAllTransaction(Integer id, User user) throws MyException {
        otherUserService.otherTransactionWithoutException(user);
        userMapper.deleteUser(id);
        throw new MyException(-1, "exception...");

    }
	@Transactional(propagation = Propagation.REQUIRED)
    public void otherTransactionWithoutException(User user) {
        userMapper.insertUserInfo(user);

    }

运行结果:

由运行结果可以看到事务2也被回滚,插入失败,所以事务2加入事务1中,与事务1一起提交一起回滚

  1. 方法1没有被事务修饰,方法2被事务修饰
/**
     * 只会回滚被事务管理的方法
     *
     * @throws MyException
     */
    @Test
    public void otherNoTransaction() throws MyException {
        User user = new User(11, "abcd", "1234567890");
        userService.otherNoTransaction(9, user);

    }
	public void otherNoTransaction(Integer id, User user) throws MyException {
        userMapper.deleteUser(id);
        otherUserService.otherTransaction(user);
    }
	@Transactional(rollbackFor = Exception.class)
    public void otherTransaction(User user) throws MyException {
        userMapper.insertUserInfo(user);
        throw new MyException(-1, "exception...");
    }

运行结果:
在这里插入图片描述
有运行结果可知,方法2被回滚。若当前不存在事务,会新建一个事务

PROPAGATION_SUPPORTS 运行结果

	@Test
    public void otherNoTransaction() throws MyException {
        User user = new User(11, "abcd", "1234567890");
        userService.otherNoTransaction(8, user);
    }
    public void otherNoTransaction(Integer id, User user) throws MyException {
        userMapper.deleteUser(id);
        otherUserService.otherTransaction(user);

    }
@Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
    public void otherTransaction(User user) throws MyException {
        userMapper.insertUserInfo(user);
        throw new MyException(-1, "exception...");
    }

运行结果:
在这里插入图片描述
由运行结果可知,方法2抛出异常后并没有回滚,所以没有使用事务

PROPAGATION_MANDATORY

public void otherNoTransaction(Integer id, User user) throws MyException {
        userMapper.deleteUser(id);
        otherUserService.otherTransaction(user);

    }
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.MANDATORY)
    public void otherTransaction(User user) throws MyException {
        userMapper.insertUserInfo(user);
        throw new MyException(-1, "exception...");
    }

运行结果:
在这里插入图片描述
因为方法1没有使用事务,所以抛出了异常

PROPAGATION_REQUIRES_NEW

@Test
    public void otherAllTransaction() throws MyException {
        User user = new User(10, "abcd", "1234567890");
        userService.otherAllTransaction(7, user);

    }
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void otherAllTransaction(Integer id, User user) throws MyException {
        otherUserService.otherTransactionWithoutException(user);
        userMapper.deleteUser(id);
        throw new MyException(-1, "exception...");

    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void otherTransactionWithoutException(User user) {
        userMapper.insertUserInfo(user);

    }

运行结果:
在这里插入图片描述
由运行结果可知,方法2事务成功提交,方法1回滚

PROPAGATION_NOT_SUPPORTED

@Test
    public void otherAllTransaction() throws MyException {
        User user = new User(11, "abcd", "1234567890");
        userService.otherAllTransaction(10, user);

    }
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void otherAllTransaction(Integer id, User user) throws MyException {
        otherUserService.otherTransactionWithoutException(user);
        userMapper.deleteUser(id);
    }
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void otherTransactionWithoutException(User user) throws MyException {
        userMapper.insertUserInfo(user);
        throw new MyException(-1, "exception...");

    }

运行结果:
在这里插入图片描述
由运行结果可知,方法2没有回滚,所以没有被事务管理,方法1被事务管理,发生了回滚

PROPAGATION_NEVER

@Test
    public void otherAllTransaction() throws MyException {
        User user = new User(13, "abcd", "1234567890");
        userService.otherAllTransaction(10, user);

    }
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void otherAllTransaction(Integer id, User user) throws MyException {
        otherUserService.otherTransactionWithoutException(user);
        userMapper.deleteUser(id);
    }
    @Transactional(propagation = Propagation.NEVER)
    public void otherTransactionWithoutException(User user) throws MyException {
        userMapper.insertUserInfo(user);
        throw new MyException(-1, "exception...");

    }

运行结果:
在这里插入图片描述
由于方法1被事务管理,所以运行报错

PROPAGATION_NESTED

并没有成功的实现方法2回滚,方法1成功提交的现象,每次都是两个方法同时回滚。
链接:link

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值