1221221

通过打印台发布招聘前端信息举例知乎

  返回  

@Transaction和try catch

2021/8/20 14:44:46 浏览:

​ 对于@Transactional可以保证RuntimeException错误的回滚,如果想保证非RuntimeException错误的回滚,需要加上rollbackFor = Exception.class 参数

​ try catch 对回滚本身没影响,但是会对异常是否可以被@Transactional感知到有影响,

​ 1.可以将try catch到的错误向上抛,就可以起作用

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(QuotaBusiness quotaBusiness) {
        try {
            Integer id = quotaBusiness.getId();
            if (id == null) {
                return Result.failure("id不能为空!");
            }
            quotaBusinessDao.deleteById(id);
            quotaBusinessDao.insert(quotaBusiness);//错误代码
        } catch (Exception e) {
            throw e;
        }
        return Result.success();
    }

​ 2.如果将try catch去掉,只留@Transactional(rollbackFor = Exception.class),可以发生回滚操作

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(QuotaBusiness quotaBusiness) {
            Integer id = quotaBusiness.getId();
            if (id == null) {
                return Result.failure("id不能为空!");
            }
            quotaBusinessDao.deleteById(id);
            quotaBusinessDao.insert(quotaBusiness);//错误代码
        return Result.success();
    }

​ 3。而当去除@Transactional(rollbackFor = Exception.class),只留下try catch中的手动回滚,是回滚不了的

    @Override
    public Result delete(QuotaBusiness quotaBusiness) {
        try {
            Integer id = quotaBusiness.getId();
            if (id == null) {
                return Result.failure("id不能为空!");
            }
            quotaBusinessDao.deleteById(id);
            quotaBusinessDao.insert(quotaBusiness);//错误代码
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return Result.success();
    }

4.而当我们想要再发生异常时返回自定义错误提示信息时,用throw、@transcation是不行的,需要手动回滚之后再return自定义信息

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(QuotaBusiness quotaBusiness) {
        try {
            Integer id = quotaBusiness.getId();
            if (id == null) {
                return Result.failure("id不能为空!");
            }
            quotaBusinessDao.deleteById(id);
            quotaBusinessDao.insert(quotaBusiness);//错误代码
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.failure("删除失败!");
        }
        return Result.success();
    }

5.@Transactional(rollbackFor = Exception.class) 和 @Transactional也是有区别的,当不加rollbackFor = Exception.class时,无论内外报RuntimeException,都会回滚,非RuntimeException则不会回滚;如果加上rollbackFor = Exception.class时,无论怎么报错都会回滚

    @Override
    @Transactional  //第一种情况
		@Transactional(rollbackFor = Exception.class)  //第二种情况
    public Result delete(QuotaBusiness quotaBusiness) {
        try {
            Integer id = quotaBusiness.getId();
            if (id == null) {
                return Result.failure("id不能为空!");
            }
            quotaBusinessDao.deleteById(id);
            quotaBusinessDao.insert(quotaBusiness);//错误代码
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.failure("删除失败!");
        }
        return Result.success();
    }

结论:

​ 对于@Transactional可以保证RuntimeException错误的回滚,如果想保证非RuntimeException错误的回滚,需要加上rollbackFor = Exception.class 参数。
​ try catch只是对异常是否可以被@Transactional 感知 到有影响。如果错误抛到切面可以感知到的地步,那就可以起作用。

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号