Spring事务@Transactional中使用异步线程bug

来源:互联网 发布:矩阵开根号 编辑:程序博客网 时间:2024/06/18 09:22
   @Transactional
    @Override

    public int genRecord(AppRecord appRecord) throws BizException{

 

         //异步线程初始化RecordStatus对象

         RecordStatusWorker recordStatusWorker = new RecordStatusWorker(appRecord);
            asyncRecordStatusAndSwipeCardExecutor.submit(recordStatusWorker);

  }


异步线程,最终调用的是这个方法

public void initRecordStatusWhenSyncRecord(Integer id) {
AppointmentRecord appointmentRecord = appointmentRecordMapper.get(id);



}


在实际执行过程中,appointmentRecord  有时为null,有时不为null。

根本原因是,事务还没有提交完成,可能就调用了initRecordStatusWhenSyncRecord方法。

这个时候,数据库中不一定有record对象。


方法1:

initRecordStatusWhenSyncRecord 查找不到Reord的时候,等几秒,再查询1次,从而完成初始化。

方法2:

调用initRecordStatusWhenSyncRecord 的时候,传递 AppRecord对象,而不是id。


原创粉丝点击