自定义异常回滚@Transactional注解
作者:互联网
//重点关注的是@Transactional注解和TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
@Override @Transactional(rollbackFor = RuntimeException.class) public ServerResp<Object> updateDistributionRule(UpdateDistributionRuleRequest req) { ServerResp<Object> resp = new ServerResp<>(); try { JSONArray jsonArray = JSONArray.parseArray(req.getDistributionList()); if (null == jsonArray ){ return resp.error("分配数组为空"); } List<TfCustomerDistributeRole> list = JSON.parseArray(jsonArray.toJSONString(), TfCustomerDistributeRole.class); for (TfCustomerDistributeRole distributeRole : list){ Integer distributeCount = tfCustomerDistributeRoleMapper.updateDistributeByStoreIdAndclerkId(distributeRole); if (distributeCount == 0){ throw new RuntimeException("修改店员分配规则错误,回滚"); } } return resp.success("修改成功"); }catch (RuntimeException e) { //触发强制回滚 e.printStackTrace(); logger.log(MyLogUtil.LOG_ERROR, "触发回滚,入参:" + req + ",错误类型" ,e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return resp.error("修改店员分配规则出现错误"); }catch (Exception e){ e.printStackTrace(); logger.log(MyLogUtil.LOG_ERROR,"修改分配规则错误"); return resp.error("分配规则修改错误"); } }
标签:ServerResp,回滚,return,自定义,Transactional,req,resp 来源: https://www.cnblogs.com/bzd1030806032/p/16301850.html