当前位置: 首页 > 图灵资讯 > 技术篇> 226-service层手动提交事务,手动回滚

226-service层手动提交事务,手动回滚

来源:图灵教育
时间:2023-12-05 17:08:02

    @Autowired    private PlatformTransactionManager transactionManager;

try的整个方法-catch

public BaseResp xxx() throws Exception{    BaseResp resp = new BaseResp();    TransactionStatus transactionStatus = null;    try{         transactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());        ///其他处理        //...                 transactionManager.commit(transactionStatus);    }catch(Exception e){        if (transactionStatus != null) {            transactionManager.rollback(transactionStatus);        }        throw e;    }    return resp;}