Browse Source

lock logs

deason 1 year ago
parent
commit
5db70742e5

+ 15 - 7
src/main/java/com/qmth/exam/reserve/service/impl/ExamReserveServiceImpl.java

@@ -25,6 +25,7 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.FastDateFormat;
+import org.redisson.api.RLock;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -33,7 +34,6 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.text.ParseException;
 import java.util.*;
-import java.util.concurrent.locks.Lock;
 import java.util.stream.Collectors;
 
 @Service
@@ -100,7 +100,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
 
         // 考生预约操作锁
         String studentApplyLockKey = String.format(CacheConstants.LOCK_STUDENT_APPLY, student.getId());
-        Lock studentApplyLock = concurrentService.getLock(studentApplyLockKey);
+        RLock studentApplyLock = (RLock) concurrentService.getLock(studentApplyLockKey);
         try {
             if (!studentApplyLock.tryLock()) {
                 log.warn("获取锁失败,同一个考生不允许同时操作预约!lockKey:{}", studentApplyLockKey);
@@ -187,9 +187,13 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             }
         } finally {
             try {
-                studentApplyLock.unlock();
+                // 解锁前检查当前线程是否持有该锁
+                if (studentApplyLock.isLocked() && studentApplyLock.isHeldByCurrentThread()) {
+                    studentApplyLock.unlock();
+                    log.info("解锁成功!lockKey:{}", studentApplyLockKey);
+                }
             } catch (Exception e) {
-                log.warn(e.getMessage());
+                log.warn("解锁失败!lockKey:{} err:{}", studentApplyLockKey, e.getMessage());
             }
         }
     }
@@ -243,7 +247,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
 
         // 考生预约操作锁
         String studentApplyLockKey = String.format(CacheConstants.LOCK_STUDENT_APPLY, student.getId());
-        Lock studentApplyLock = concurrentService.getLock(studentApplyLockKey);
+        RLock studentApplyLock = (RLock) concurrentService.getLock(studentApplyLockKey);
         try {
             if (!studentApplyLock.tryLock()) {
                 log.warn("获取锁失败,同一个考生不允许同时操作预约!lockKey:{}", studentApplyLockKey);
@@ -274,9 +278,13 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             log.warn("取消预约成功!studentId:{} examSiteId:{} timePeriodId:{}", student.getId(), examSiteId, timePeriodId);
         } finally {
             try {
-                studentApplyLock.unlock();
+                // 解锁前检查当前线程是否持有该锁
+                if (studentApplyLock.isLocked() && studentApplyLock.isHeldByCurrentThread()) {
+                    studentApplyLock.unlock();
+                    log.info("解锁成功!lockKey:{}", studentApplyLockKey);
+                }
             } catch (Exception e) {
-                log.warn(e.getMessage());
+                log.warn("解锁失败!lockKey:{} err:{}", studentApplyLockKey, e.getMessage());
             }
         }
     }