解决controller层Exception异常事务回滚失效

Spring的@Transactional源码中写道:
By default, a transaction will be rolling back on {@link RuntimeException}and {@link Error} but not on checked exceptions (business exceptions).

默认情况下,如果在事务中抛出了未检查异常(继承自 RuntimeException 的异常)或者 Error,则 Spring 将回滚事务;除此之外,Spring 不会回滚事务。
在这里插入图片描述

测试①:
默认spring事务只在发生未被捕获的 RuntimeException 时才回滚。

// 测试回滚成功案例,基于IllegalArgumentException(RuntimeException)实现回滚
@GetMapping("/testSuccess")
@Transactional// 如果不加,就不会回滚
public R testSuccess(@RequestParam("type") Integer type){

    eduTeacherService.removeById("2");

    if (type == 1){
        throw new IllegalArgumentException("测试回滚成功案例!");
    }

    eduTeacherService.removeById("3");

    return  R.ok();
}

在这里插入图片描述

测试②:
Exception异常,事务回滚失败;

// 测试回滚失败案例,基于Exception实现回滚;
@GetMapping("/testFail")
@Transactional
public R testFail(@RequestParam("type") Integer type)  {

    try {

        eduTeacherService.removeById("2");

        if (type == 1){
            throw new Exception("测试回滚失败案例!");
        }

        eduTeacherService.removeById("3");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return  R.ok();
}

在这里插入图片描述

测试③:
用rollbackFor解决Exception不进行事务回滚
rollbackFor = Exception.class + throws Exception

@GetMapping("/testFailRollbackFor")
// 配置rollbackFor
@Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
public R testFailRollbackFor(@RequestParam("type") Integer type) throws Exception {

        eduTeacherService.removeById("2");

        if (type == 1){
            throw new Exception("测试回滚失败rollbackFor成功案例!");
        }

        eduTeacherService.removeById("3");

    return  R.ok();
}

在这里插入图片描述

测试④:
手动回滚解决Exception不进行事务回滚
catch:
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

@GetMapping("/testSuccessByHand")
@Transactional
public R testSuccessByHand(@RequestParam("type") Integer type)  {

    try {

        eduTeacherService.removeById("2");

        if (type == 1){
            throw new Exception("测试回滚失败案例!");
        }

        eduTeacherService.removeById("3");
    } catch (Exception e) {
        e.printStackTrace();
        //手动回滚,如果sql2()抛了异常,sql1()会回滚,不影响事物正常执行
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }

    return  R.ok();
}

在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值