From 3243c2a2f2555d5a2a0a68dffce36fd6c239a611 Mon Sep 17 00:00:00 2001 From: dshvaika Date: Mon, 8 Dec 2025 18:36:01 +0200 Subject: [PATCH] Added find before lock for the startup of application --- .../dao/sqlts/dictionary/JpaKeyDictionaryDao.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dao/src/main/java/org/thingsboard/server/dao/sqlts/dictionary/JpaKeyDictionaryDao.java b/dao/src/main/java/org/thingsboard/server/dao/sqlts/dictionary/JpaKeyDictionaryDao.java index 1ef2c2d6aa..bcb9284371 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sqlts/dictionary/JpaKeyDictionaryDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sqlts/dictionary/JpaKeyDictionaryDao.java @@ -50,6 +50,15 @@ public class JpaKeyDictionaryDao extends JpaAbstractDaoListeningExecutorService if (cached != null) { return cached; } + var compositeKey = new KeyDictionaryCompositeKey(strKey); + Optional entryOpt = keyDictionaryRepository.findById(compositeKey); + if (entryOpt.isPresent()) { + Integer keyId = entryOpt.get().getKeyId(); + if (keyId != null) { + keyDictionaryMap.put(strKey, keyId); + return keyId; + } + } creationLock.lock(); try { Integer keyId = keyDictionaryMap.get(strKey); @@ -59,8 +68,7 @@ public class JpaKeyDictionaryDao extends JpaAbstractDaoListeningExecutorService keyId = keyDictionaryRepository.upsertAndGetKeyId(strKey); if (keyId == null || keyId == 0) { log.warn("upsertAndGetKeyId returned: [{}] for key: [{}], falling back to findById", keyId, strKey); - KeyDictionaryCompositeKey id = new KeyDictionaryCompositeKey(strKey); - Optional entryOpt = keyDictionaryRepository.findById(id); + entryOpt = keyDictionaryRepository.findById(compositeKey); if (entryOpt.isEmpty() || entryOpt.get().getKeyId() == null || entryOpt.get().getKeyId() == 0) {