The query datagram is null when the data is not inserted into the database

What was especially funny today was that the program reported an error. The test told me that the time when querying a driver's fee information was abnormal, and then the query would be fine after a while. The abnormality was not reported, and then I started to look at the log.

The log error is as follows

Then I found out the code

	//获取司机费用明细		RpcOrderFeeDetailVo driverOrderFee = getDrivingOrderFeeDetail(drivingOrderDto, 2);		rpcGetOrderFeeDetailVo.setDriverFeeDetail(driverOrderFee); 		//平台费		double userTotalRealFee = Optional.ofNullable(userOrderFee).map(u -> u.getTotalRealFee()).orElse(0.0);		double driverTotalRealFee = Optional.ofNullable(driverOrderFee).map(u -> u.getTotalRealFee()).orElse(0.0);		double platFee = userTotalRealFee - driverTotalRealFee;		if (platFee != 0.0) {			driverOrderFee.setPlatFee(CurrencyUtil.formatDouble2(platFee));		} 		//查询平台费上限		RpcChargingConfigOutDto chargingConfigOutDto = null;		try {			RealPriceParamDto priceParamDto = new RealPriceParamDto();			priceParamDto.setOrderType(drivingOrderDo.getOrderType());			priceParamDto.setChannelType(1);			priceParamDto.setCityCode(drivingOrderDo.getCityCode());			priceParamDto.setStartTime(drivingOrderDo.getGetUpTime());			chargingConfigOutDto = valuationService.getChargingCityConfig(priceParamDto);		}catch (Exception ex){			logger.error("rpc平台费上限获取失败");		}		//计算司机补偿		double driverComp = 0.0;		if(chargingConfigOutDto != null && chargingConfigOutDto.getServiceCharegeMax() != null) {			double ChargeMaxF = CurrencyUtil.changeF2Y(chargingConfigOutDto.getServiceCharegeMax());			if (platFee > ChargeMaxF) {				driverComp = CurrencyUtil.formatDouble2(platFee - ChargeMaxF);				driverOrderFee.setPlatFee(ChargeMaxF); 				driverOrderFee.setDriverIncome(driverComp);			}else{				driverOrderFee.setDriverIncome(0.0);			}		}else {			driverOrderFee.setDriverIncome(0.0);		}
	/**	 * 用户、司机费用明细	 *	 * @param drivingOrderDto	 * @param type	 * @return	 */	private RpcOrderFeeDetailVo getDrivingOrderFeeDetail(DrivingOrderInDto drivingOrderDto, Integer type) {		RpcOrderFeeDetailVo rpcOrderFeeDetailVo = new RpcOrderFeeDetailVo(); 		Example example = new Example(DrivingOrderExpenseDo.class);		example.createCriteria().andEqualTo("orderId", drivingOrderDto.getOrderId()).andEqualTo("expenseType", type);		example.orderBy("createTime").desc();		List<DrivingOrderExpenseDo> drivingOrderExpenseDoList = drivingOrderExpenseMapper.selectByExample(example);		if (CollectionUtils.isEmpty(drivingOrderExpenseDoList)) {			return null;		} else {			BeanUtils.copyProperties(drivingOrderExpenseDoList.get(0), rpcOrderFeeDetailVo); 			// 等候时长			int waitTimes = Optional.ofNullable(drivingOrderExpenseDoList.get(0).getWaitTimes()).orElse(0);			rpcOrderFeeDetailVo.setWaitTimes(waitTimes); 			// 优惠券			int coupon = Optional.ofNullable(drivingOrderExpenseDoList.get(0).getCoupon()).orElse(0);			rpcOrderFeeDetailVo.setCoupon(coupon == 0 ? 0.0 : CurrencyUtil.changeF2Y(coupon)); 			// 保险费用			rpcOrderFeeDetailVo.setInsuranceFee(CurrencyUtil.changeF2Y(Optional.ofNullable(drivingOrderExpenseDoList.get(0).getInsuranceFee()).orElse(0))); 			// 等候费			int waitFee = Optional.ofNullable(drivingOrderExpenseDoList.get(0).getWaitTimeFee()).orElse(0);			rpcOrderFeeDetailVo.setWaitTimeFee(waitFee == 0 ? 0.0 : CurrencyUtil.changeF2Y(waitFee)); 			// 等候时长			rpcOrderFeeDetailVo.setLongDistanceFee(CurrencyUtil.changeF2Y(Optional.ofNullable(drivingOrderExpenseDoList.get(0).getLongDistanceFee()).orElse(0))); 			// 时长费			rpcOrderFeeDetailVo.setLongTimeFee(CurrencyUtil.changeF2Y(Optional.ofNullable(drivingOrderExpenseDoList.get(0).getLongTimeFee()).orElse(0))); 			// 起步价			int startPrice = Optional.ofNullable(drivingOrderExpenseDoList.get(0).getStartPrice()).orElse(0);			rpcOrderFeeDetailVo.setStartPrice(startPrice == 0 ? 0.0 : CurrencyUtil.changeF2Y(startPrice)); 			// 里程费			int mileageFee = Optional.ofNullable(drivingOrderExpenseDoList.get(0).getOrderMileageFee()).orElse(0);			rpcOrderFeeDetailVo.setOrderMileageFee(mileageFee == 0 ? 0.0 : CurrencyUtil.changeF2Y(mileageFee)); 			// 总的实际费用			int totalRealFee = Optional.ofNullable(drivingOrderExpenseDoList.get(0).getTotalRealFee()).orElse(0);			rpcOrderFeeDetailVo.setTotalRealFee(totalRealFee == 0 ? 0.0 : CurrencyUtil.changeF2Y(drivingOrderExpenseDoList.get(0).getTotalRealFee())); 			// 其他费用			int otherFee = Optional.ofNullable(drivingOrderExpenseDoList.get(0).getOtherFee()).orElse(0);			rpcOrderFeeDetailVo.setOtherFee(CurrencyUtil.changeF2Y(otherFee)); 		} 		return rpcOrderFeeDetailVo;	}

I really can’t think of which word will report an error, and why it will be fine after a while. Then I asked an experienced colleague. The colleague looked at the code and said directly that if this line reports an exception, it must be the object driverOrderFee is null, and the class is Where did it come from? Later, I found it from the database, and then asked me to look at the reason why it could not be found in the database. Later, I was still curious about why there would be data in the database after a while, and then the colleague said:

You see if there is any asynchronous situation, if the order has been placed, but the data is checked before the data is in the database, or if there is a message sent by Kafka, the message has not been consumed when the message is checked No storage, or whether it is executed regularly, causing the data to be queried before it is stored

solve

Later I took a look at the code. The driver will insert a piece of cost information in the database when the trip is over, and will not insert it in other order states. Later, I asked if I would check it when the trip was not over. The driver’s fee information, the other party said yes, and later this was not changed as a bug

Experienced and inexperienced are not the same