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

@Transactional catch异常手动回滚

程序员文章站 2022-07-04 22:09:57
...

@Transactional注解手动回滚

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
如下:

@Transactional(rollbackFor = Exception.class)
    public Result<String> transactionalTest(String id) {
        Result.ResultBuilder<String> builder = Result.builder();
        try {
            //业务代码
            return builder.success(STAUTS_SUCCESS).msg(MSG_SUCCESS).data(STAUTS_SUCCESS).build();
        } catch (Exception e) {
            log.error("异常信息为:", e);
            //手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            //捕获异常回滚后返回相同格式数据
            return builder.success(STAUTS_FAIL).msg(MSG_FAIL).data(null).build();
        }
    }