li-shaoke
li-shaoke
boolean isSuccess = redisTemplate.opsForValue().setIfAbsent(businessKey, uniqueValue); if (!isSuccess) { throw new Exception("You can't do it,because another has get the lock =-="); } redisTemplate.expire(businessKey, annotation.lockTime(), TimeUnit.SECONDS); 这块redis操作,不是原子的,如果在设置过期时间前,突然宕机了,则加锁后的key将长时间存在, 应该用 Boolean setIfAbsent(K key, V...
finally { // 请求结束后,强制删掉 key,释放锁 redisTemplate.delete(businessKey); log.info("release the lock, businessKey is [" + businessKey + "]"); } 如果A线程锁操作超时了,但是还在执行,此时B线程进入了拿到锁并进行操作,此时A线程去释放锁,释放的就是B此时持有的锁。 此处应该加入判断,来释放锁的线程是否是持有锁的那个线程。 可以改成 finally { try { RedisLockDefinitionHolder redisLockDefinitionHolder = holderList.stream().filter(h -> businessKey.equals(h.getBusinessKey())).findFirst().orElse(null);...