赞
踩
使用redis的分布式锁出现了下面的错误:
attempt to unlock lock, not locked by current thread by node id: 83394267-6fff-450c-8cee-2690f845ec4f thread-id: 89
有问题的代码:
- //从redis查不到,则走mysql
- String key = getLiveFaceResourceStLockKey(region);
- RLock rLock = redissonRedisService.getLock(key);
- try {
- log.info("live_face_resource_st_from_mysql_get_lock");
- boolean bool = rLock.tryLock(RedisKey.EXPIRE_TEN_SECONDS, TimeUnit.SECONDS);
-
- if (bool) {
-
- List<LiveFaceResourceSt> stList = liveFaceResourceStService.selectListOnline(region, platform);
- resourcesResponseList.addAll(ListUtils.emptyIfNull(stList).stream()
- .filter(Objects::nonNull)
- .map(this::convert)
- .collect(Collectors.toList()));
- if (CollectionUtils.isNotEmpty(resourcesResponseList)) {
- micoRedisService.string().set(getLiveFaceResourceStKey(region), JSON.toJSONString(resourcesResponseList));
- micoRedisService.expire(getLiveFaceResourceStKey(region), RedisKey.EXPIRE_FIVE_MINUTE, TimeUnit.SECONDS);
- }
-
- }
- } catch (Exception e) {
- log.error("live_face_resource_st_from_mysql_lock_fail:", e);
- } finally {
- if (rLock.isLocked()) {
- try {
- rLock.unlock();
- } catch (Exception e) {
- log.error("live_face_resource_st解锁异常:", e);
- }
- }
- }
修复后的代码:
- //从redis查不到,则走mysql
- String key = getLiveFaceResourceStLockKey(region, platform);
- RLock lock = redissonRedisService.getLock(key);
- lock.lock(10, TimeUnit.SECONDS);
- try {
- List<LiveFaceResourceSt> stList = liveFaceResourceStService.selectListOnline(region, platform);
- resourcesResponseList.addAll(ListUtils.emptyIfNull(stList).stream()
- .filter(Objects::nonNull)
- .map(this::convert)
- .collect(Collectors.toList()));
-
- if (CollectionUtils.isNotEmpty(resourcesResponseList)) {
- micoRedisService.string().set(getLiveFaceResourceStKey(region, platform), JSON.toJSONString(resourcesResponseList));
- micoRedisService.expire(getLiveFaceResourceStKey(region, platform), RedisKey.EXPIRE_FIVE_MINUTE, TimeUnit.SECONDS);
- }
- } catch (Exception e) {
- log.error("live_face_resource_st_from_mysql_lock_fail:", e);
- } finally {
- if (lock.isLocked()) {
- lock.unlock();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。