java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 48c213c9-1945-4c1b-821e-6d32e347eb44 thread-id: 69
出错代码:
java
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 中加入如下判断即可
java
finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}