美文网首页
@Transactional回滚问题(try catch)

@Transactional回滚问题(try catch)

作者: 虾米咬小米 | 来源:发表于2021-03-02 13:55 被阅读0次

Spring 事务注解 @Transactional 本来可以保证原子性,如果事务内有报错的话,整个事务可以保证回滚,但是加上try catch或者事务嵌套,可能会导致事务回滚失败。测试一波。

最简单测试
如果我们单纯@Transactional,事务可以正常回滚吗?

    @GetMapping("/saveNormal0")
    @Transactional
    public void saveNormal0() throws Exception {
        int age = random.nextInt(100);
        User user = new User().setAge(age).setName("name:"+age);
        userService.save(user);
        throw new RuntimeException();
    }

复制代码如果事务内报了RuntimeException错误,事务可以回滚。

    @GetMapping("/saveNormal0")
    @Transactional
    public void saveNormal0() throws Exception {
        int age = random.nextInt(100);
        User user = new User().setAge(age).setName("name:"+age);
        userService.save(user);
        throw new Exception();
    }

复制代码如果事务内报了Exception错误(非RuntimeException错误),事务不可以回滚。

    @GetMapping("/saveNormal0")
    @Transactional( rollbackFor = Exception.class)
    public void saveNormal0() throws Exception {
        int age = random.nextInt(100);
        User user = new User().setAge(age).setName("name:"+age);
        userService.save(user);
        throw new Exception();
    }

复制代码如果是Exception错误(非RuntimeException),加上 rollbackFor = Exception.class 参数也可以实现回滚。

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

发现try catch对回滚这个事本身没有什么影响,结论一照样成立。try catch只是对异常是否可以被@Transactional 感知 到有影响。如果错误抛到切面可以感知到的地步,那就可以起作用。

项目中是这样处理的:

  @Override
    @Transactional
    public boolean deleteCase(CaseEntity caseEntity) {
        if (StringUtil.isEmpty(caseEntity.getId())) throw new BusinessException("Id not null");

        try {

            iCaseThhzService.update(new UpdateWrapper<CaseThhzEntity>()
                    .set("IS_ENABLED", CommonConstant.IsUse.NO.getName())
                    .eq("CASE_ID", caseEntity.getId()));




            return this.update(new UpdateWrapper<CaseEntity>()
                    .set("IS_ENABLED", CommonConstant.IsUse.NO.getName())
                    .set(!StringUtils.isEmpty(caseEntity.getDeleteRemark()), "DELETE_REMARK", caseEntity.getDeleteRemark())
                    .eq("ID", caseEntity.getId()));
        } catch (Exception e) {
            log.error("系统异常", e);
            throw new BusinessException(e.getMessage());
        }
    }

BusinessException 继承自 RuntimeException 这样就可以实现回滚了

public class BusinessException extends RuntimeException implements IException,
        Serializable {

    private static final long serialVersionUID = -5814170840007770954L;
    public static final String CODE_ILLEGAL_PARAMS = "10003";// 不合法的参数




    protected String errCode;
    protected Integer Code;
    private String errMsg;
    private transient Object[] arguments;

    public BusinessException() {

    }

    public BusinessException(String msg) {
        super(msg);
        this.errMsg = msg;
    }

    public BusinessException(String msg, Throwable cause) {
        super(msg, cause);
    }

    public BusinessException(String code, String msg) {
        super(msg);
        this.errMsg = msg;
        this.errCode = code;
    }

    public BusinessException(Integer code, String msg) {
        super(msg);
        this.errMsg = msg;
        this.Code = code;
    }
    public BusinessException(String code, Object... args) {
        this.errCode = code;
        this.arguments = args;
    }

    public BusinessException(String code, String msg, Object... args) {
        super(msg);
        this.errMsg = msg;
        this.errCode = code;
        this.arguments = args;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public String getErrorCode() {
        // TODO Auto-generated method stub
        return errCode;
    }

    public void setErrorArguments(Object... paramVarArgs) {
        // TODO Auto-generated method stub
        this.arguments = paramVarArgs;
    }

    public Object[] getErrorArguments() {
        // TODO Auto-generated method stub
        return this.arguments;
    }
}

相关文章

网友评论

      本文标题:@Transactional回滚问题(try catch)

      本文链接:https://www.haomeiwen.com/subject/ufotqltx.html