自定义异常回滚@Transactional注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//重点关注的是@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

posted @   迷糊桃  阅读(118)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程
· SQL Server 内存占用高分析及解决办法(超详细)
· ffmpeg简易播放器(1)--了解视频格式
· 20250116 支付宝出现重大事故 有感
点击右上角即可分享
微信分享提示