自定义异常回滚@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("分配规则修改错误"); } }
@Override @Transactional() public boolean sendTfProductPublicity(Map params) { // 启用一个事务 Object obj = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); 代码··················· // 报错后 对上面创建的事务进行回滚 TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(obj); }
转自:https://www.cnblogs.com/bzd1030806032/p/16301850.html
本文来自博客园,作者:迷糊桃,转载请注明原文链接:https://www.cnblogs.com/mihutao/p/17233053.html