关于处理hibernate乐观锁抛异常ObjectOptimisticLockingFailureException问题

主要解决方案是在捕捉异常的时候使用递归方法对乐观锁重试

  public void optimisticLockingFailureSave(BizUser bizUser, RefundLog refundLog, TradeOrderLog order) {
        try {
            if (bizUser != null) {
                bizUser.setVersion(bizUserService.findOne(bizUser.getId()).getVersion());
                bizUserService.save(bizUser);
                logger.info("用户表更新成功");
            } else if (refundLog != null) {
                refundLog.setVersion(refundLogService.findOne(refundLog.getId()).getVersion());
                refundLogService.save(refundLog);
                logger.info("提现表更新成功");
            } else if (order != null) {
                order.setVersion(tradeOrderLogService.findOne(order.getId()).getVersion());
                tradeOrderLogService.save(order);
                logger.info("订单表更新成功");
            }
        } catch (Exception e) {
            if (e instanceof ObjectOptimisticLockingFailureException || e instanceof StaleObjectStateException || e instanceof TransactionSystemException) {
                if (bizUser != null) {
                    logger.info("更新用户表乐观锁重试");
                    optimisticLockingFailureSave(bizUser, null, null);
                } else if (refundLog != null) {
                    logger.info("更新提现表乐观锁重试");
                    optimisticLockingFailureSave(null, refundLog, null);
                } else if (order != null) {
                    logger.info("更新TradeOrderLog表乐观锁重试");
                    optimisticLockingFailureSave(null, null, order);
                }
            }
        }
    }



调用:
 user.setLastModifiedBy("provider:refunded");
                                           
 //更新用户表
optimisticLockingFailureSave(user, null, null);

你可能感兴趣的:(hibernate)