欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring事务的传播行为

程序员文章站 2022-05-06 21:37:53
...

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的效果相同

数据库信息

spring事务的传播行为

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);

    }

运行结果:
spring事务的传播行为
由运行结果可以看到事务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...");
    }

运行结果:
spring事务的传播行为
有运行结果可知,方法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...");
    }

运行结果:
spring事务的传播行为
由运行结果可知,方法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...");
    }

运行结果:
spring事务的传播行为
因为方法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);

    }

运行结果:
spring事务的传播行为
由运行结果可知,方法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...");

    }

运行结果:
spring事务的传播行为
由运行结果可知,方法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...");

    }

运行结果:
spring事务的传播行为
由于方法1被事务管理,所以运行报错

PROPAGATION_NESTED

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

相关标签: spring