微云村 2022-01-27 08:58 采纳率: 0%
浏览 93

多线程写,唯一索引冲突后,获取不到数据

问题描述:

多线程调用方法一,挂起事务同时写入,唯一索引冲突后(catch的error有打印),再查询有时会返回空?

相关代码:
类一方法一:
@Transactional
public void addReturnNum(Long fWarehouseId, MqAmazonSettleResponse mqResponse, Long accountingEntityId, Long amazonSettleId) {
    QueryWrapper<FProductInventory> find = new QueryWrapper<>();
    find.eq("product_id",mqResponse.getProductId());
    find.eq("warehouse_id",fWarehouseId);
    find.eq("accounting_entity_id",accountingEntityId);
    FProductInventory productInventory = getBaseMapper().selectOne(find);
    if(productInventory == null){
        //调用类二方法二
        productInventory = getFProductInventory(fWarehouseId,mqResponse.getProductId(),mqResponse.getProductCode(),mqResponse.getProductName(),accountingEntityId);
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("inventoryId",productInventory.getId()); //报空指针了
    jsonObject.put("num",mqResponse.getPlanNum());
}

类二方法二:
@Transactional(propagation = Propagation.NOT_SUPPORTED, rollbackFor = Exception.class)
public FProductInventory getFProductInventory(Long warehouseId, Long productId,String productCode,String productName,Long accEnId) {
    //创建
    LocalDateTime now = LocalDateTime.now();
    FProductInventory obj = FProductInventory.builder()
            .accountingEntityId(accEnId)
            .warehouseId(warehouseId)
            .productId(productId)
            .productCode(productCode)
            .productName(productName)
            .build();
    //有添加唯一索引,插入可能会报错
    try {
        baseMapper.insert(obj);
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        log.error("新增财务库存异常:仓库"+warehouseId+",产品"+productId+e.getMessage());
    }
    return baseMapper.selectOneNew(warehouseId,productId);
}
思路:

是否唯一索引生效的时候,还查询不到,存在一定的时间差?

  • 写回答

1条回答 默认 最新

  • 於黾 2022-01-27 09:07
    关注

    返回空的时候应该抛错误了吧,在查询的地方打个断点,或者好好查查日志,你不是把catch信息全都存日志了吗
    你用事务锁了表,那么查询的时候肯定查不到了呀

    评论

报告相同问题?

问题事件

  • 修改了问题 1月28日
  • 修改了问题 1月27日
  • 修改了问题 1月27日
  • 创建了问题 1月27日

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题