• 事务中发送消息产生的问题


    这次的场景是,外部模块接受到消息会调用我们的回调接口,更新发送消息前插入的数据,但现在出现了插入的数据找不到的情况,而进入数据库查看插入的数据是存在的

    @Transctional
    public CmsCustomerDetailResult getDetail(){
    //操作数据库,插入数据
    ...
    sendKafkaMessage();
    
    }
    
    
    

    **猜测原因:发kafka消息是瞬发的,而插入数据库的操作被注解式事务锁定,要发送消息后才提交,如果外部系统回调太快就会出现这种情况 **

    修改后:取消注解式事务,使用带返回值的编程式事务缩小事务的粒度,提交后再发消息

     Boolean execute = transactionTemplate.execute(transactionStatus -> {
                dao.insert(customerMergeRecordEntity);
                dao.insertBatch(feedbackRecordEntityList);
                dao.updateFields(customerEntity, true, "isEnable", "customerPhaseType");
                return true;
            });
            if (execute) {
              sendKafkaMessage();
            }
    
    

    修改后问题还存在,发现是某个外部模块消费过快,几乎是马上返回,没办法了,只能在sendKafkaMessage()之前加上sleep(500)睡半秒,问题解决

  • 相关阅读:
    Fortran编译器之一GUN Fortran安装(Windows XP)
    c++动态绑定的技术实现
    c++标准库比较
    java array
    java常用的基础容器
    mac sublime text 3 add ctags plugin
    git fetch
    查看远程分支的log
    git删除分支
    detached HEAD state
  • 原文地址:https://www.cnblogs.com/CodeSpike/p/13474033.html
Copyright © 2020-2023  润新知