问题描述:
多线程调用方法一,挂起事务同时写入,唯一索引冲突后(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);
}
思路:
是否唯一索引生效的时候,还查询不到,存在一定的时间差?