@Transactional catch异常手动回滚

@Transactional注解手动回滚

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

@Transactional(rollbackFor = Exception.class)
    public Result transactionalTest(String id) {
        Result.ResultBuilder 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();
        }
    }

 

你可能感兴趣的:(springBoot)