自定义异常回滚@Transactional注解
Posted 迷糊桃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义异常回滚@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
以上是关于自定义异常回滚@Transactional注解的主要内容,如果未能解决你的问题,请参考以下文章
spring 使用事务注解@Transactional 异常捕获事务失效的问题
SpringBoot 之@Transactional事务回滚