TRX 11 месяцев назад
Родитель
Сommit
7a461d317a

+ 14 - 2
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/base/CommonService.java

@@ -192,6 +192,17 @@ public class CommonService {
         return base64;
     }
 
+    public boolean isAcquire(String id, Long expiration, SuperMain superMain) {
+        if (StringUtils.isEmpty(id) || ObjectUtils.isEmpty(superMain)) {
+            return false;
+        }
+        if (expiration == null) {
+            expiration = 5000L;
+        }
+        String lockKey = acquire(id, expiration, superMain);
+        return StringUtils.isEmpty(lockKey);
+    }
+
     public String acquire(String id, Long expiration, SuperMain superMain) {
         Query query = Query.query(Criteria.where("_id").is(id).and("token").isNull());
         String token = TokenUtil.create();
@@ -213,11 +224,12 @@ public class CommonService {
     }
 
     public boolean release(String id, SuperMain superMain) {
+        log.info("release {}", id);
         Query releaseQuery = Query.query(Criteria.where("_id").is(id));
         Update releaseUpdate = new Update().set("expireAt", null).set("token", null);
-        FindAndModifyOptions releaseOptions = new FindAndModifyOptions().upsert(true).returnNew(true);
+        FindAndModifyOptions releaseOptions = new FindAndModifyOptions().upsert(false).returnNew(true);
         SuperMain flowDisposition = mongoTemplate.findAndModify(releaseQuery, releaseUpdate, releaseOptions, superMain.getClass());
-        return StringUtils.isEmpty(flowDisposition.getToken());
+        return ObjectUtils.isNotEmpty(flowDisposition) && StringUtils.isEmpty(flowDisposition.getToken());
     }
 
 }

+ 1 - 3
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/payment/ExpenseFlowServiceImpl.java

@@ -1177,9 +1177,7 @@ public class ExpenseFlowServiceImpl extends SuperService implements ExpenseFlowS
         }
         String id = expenseFlow.getId();
         try {
-            String lockKey = commonService.acquire(id, 5000L, expenseFlow);
-            log.info("lockKey: {}", lockKey);
-            if (StringUtils.isNotEmpty(lockKey)) {
+            if (commonService.isAcquire(id, 5000L, expenseFlow)) {
                 if (expenseFlow.getOrderState() == OrderState.WAIT_PAYMENT
                         && expenseFlow.getExpirationTime() <= System.currentTimeMillis()) {
                     // 为待支付并且支付时间已过