分布式事务的问题

来源:9-12 分布式事务02-编码实现

梅村

2022-10-26

我在消息接收端故意创建一个错误
int 1=1/0

  @Override
    @Transactional(rollbackFor = Exception.class)
    public void onMessage(UserAddBonusMsgDTO message) {
        //当收到消息的时候执行的业务
        //1 为用户加积分
        Integer userId = message.getUserId();
        User user = this.userMapper.selectByPrimaryKey(userId);
        user.setBonus(user.getBonus() + message.getBonus());
        this.userMapper.updateByPrimaryKey(user);
        //2记录到bonus_envet_log 表里边
        this.bonusEventLogMapper.insert(BonusEventLog
                .builder()
                .userId(userId)
                .value(message.getBonus())
                .event("ConTRIBUTE")
                .createTime(new Date())
                .description("投稿加积分")
                .build());

        int i = 1 / 0;
    }

在 用postman 调用 http://127.0.0.1:8082/admin/shares/audit/1
参数为:
{"auditStatusEnum":"PASS", "reason":"优质资源" }
现在的问题消息是发过去了 用户user端没执行成功,但是内容中心还是执行提示消息成功发送并执行了数据库操作

  Preparing: SELECT show_flag,audit_status,reason FROM share WHERE id = ? 
 Parameters: 1(Integer)
      Total: 1
 Parameters: 47d0658e-f2a2-4bb1-a69a-08bd3fd9b076(String)
  Preparing: UPDATE share SET audit_status = ?,reason = ? WHERE id = ? 
 Parameters: PASS(String), 优质资源(String), 1(Integer)
      Total: 0
    Updates: 1
    Updates: 1

分布式事务没起作用 旺大目老师提供个解决方案

写回答

1回答

大目

2022-11-08

您好,这种方案能实施的前提,是消费者一侧有能力成功消费消息,否则是会不一致的。

所以这个方案也叫基于可靠消息的分布式事务,前提是消息可靠

0
0

Spring Cloud Alibaba微服务从入门到进阶

面向未来微服务:熟练掌握Spring Cloud Alibaba

3083 学习 · 1324 问题

查看课程