fix(OrderServiceImpl.java): tryLock释放锁先判断锁是否已经被释放

This commit is contained in:
郝先瑞 2022-02-26 23:23:20 +08:00
parent a2a7a402d3
commit f8aa83539e

View File

@ -218,8 +218,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
boolean flag = false; boolean flag = false;
try { try {
//尝试获取锁获取不到会立马返回 false // 尝试加锁最多等待1秒上锁10秒后自动解锁
flag = lock.tryLock(0L, 10L, TimeUnit.SECONDS); flag = lock.tryLock(1L, 10L, TimeUnit.SECONDS);
if (!flag) { if (!flag) {
throw new BizException("订单不可重复支付"); throw new BizException("订单不可重复支付");
} }
@ -232,25 +232,19 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
result = (T) wxJsapiPay(appId, order); result = (T) wxJsapiPay(appId, order);
break; break;
default: default:
case BALANCE:
result = (T) balancePay(order); result = (T) balancePay(order);
break;
} }
// 扣减库存 // 扣减库存
Result<?> deductStockResult = skuFeignClient.deductStock(order.getOrderSn()); Result<?> deductStockResult = skuFeignClient.deductStock(order.getOrderSn());
if (!Result.isSuccess(deductStockResult)) { Assert.isTrue(Result.isSuccess(deductStockResult), "扣减商品库存失败");
throw new BizException("扣减商品库存失败");
}
return result; return result;
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
throw new BizException("锁订单异常"); throw new BizException("锁订单异常");
} catch (Exception e) {
//异常继续往上抛
throw e;
} finally { } finally {
//释放锁 //释放锁
if (flag) { if (flag && lock.isLocked()) {
lock.unlock(); lock.unlock();
} }
} }
@ -268,9 +262,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
// 扣减余额 // 扣减余额
Long payAmount = order.getPayAmount(); Long payAmount = order.getPayAmount();
Result<?> deductBalanceResult = memberFeignClient.deductBalance(payAmount); Result<?> deductBalanceResult = memberFeignClient.deductBalance(payAmount);
if (!Result.isSuccess(deductBalanceResult)) { Assert.isTrue(Result.isSuccess(deductBalanceResult), "扣减账户余额失败");
throw new BizException("扣减账户余额失败");
}
// 更新订单状态 // 更新订单状态
order.setStatus(OrderStatusEnum.PAYED.getCode()); order.setStatus(OrderStatusEnum.PAYED.getCode());