redisson 释放分布式锁 踩坑

java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 48c213c9-1945-4c1b-821e-6d32e347eb44 thread-id: 69
在这里插入图片描述

出错代码:

   private void insertHourLog(Timestamp lastHourStartTimeStamp) {
        RLock lock = redissonClient.getLock("hourLogLock");
        try {
            if (lock.tryLock(70, TimeUnit.SECONDS)) {
                QueryWrapper<TdengineHourLog> tdengineHourLogQueryWrapper = new QueryWrapper<>();
                tdengineHourLogQueryWrapper.eq("belong_time", lastHourStartTimeStamp);
                TdengineHourLog tdengineHourLog = tdengineHourLogMapper.selectOne(tdengineHourLogQueryWrapper);
                LocalDateTime currentDateTime = LocalDateTime.now();
                if (tdengineHourLog != null) {
                    tdengineHourLog.setTotal(tdengineHourLog.getTotal() + 1);
                    String statusMapString = tdengineHourLog.getStatus_map();
                    if (StrUtil.isNotEmpty(statusMapString)) {
                        HashMap statusMap = JSONUtil.toBean(statusMapString, HashMap.class);
                        statusMap.put(basicConfig.nodeName, currentDateTime);
                        tdengineHourLog.setStatus_map(JSONUtil.toJsonStr(statusMap));
                        tdengineHourLogMapper.updateById(tdengineHourLog);
                        log.info("更新hourLog");
                    } else {
                        HashMap<String, Object> statusMap = new HashMap<>();
                        statusMap.put(basicConfig.nodeName, currentDateTime);
                        tdengineHourLog.setStatus_map(JSONUtil.toJsonStr(statusMap));
                        tdengineHourLogMapper.updateById(tdengineHourLog);
                        log.info("更新hourLog");
                    }
                } else {
                    tdengineHourLog = new TdengineHourLog();
                    tdengineHourLog.setBelong_time(lastHourStartTimeStamp);
                    tdengineHourLog.setTotal(1);
                    HashMap<String, Object> statusMap = new HashMap<>();
                    statusMap.put(basicConfig.nodeName, currentDateTime);
                    tdengineHourLog.setStatus_map(JSONUtil.toJsonStr(statusMap));
                    tdengineHourLogMapper.insert(tdengineHourLog);
                    log.info("新增hourLog");
                }
                lock.unlock();
            } else {
                log.info("60s超时未获取到hourLogLock");
            }
        } catch (InterruptedException e) {
            log.error("插入hour_log日志出错", e);
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

最后的finally中的 lock.unlock()存在一种情况,即未获取锁的情况下,调用了 unlock 解锁方法

在finally 中加入如下判断即可

  finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingPeppa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值