diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java index cd161d650e..9cb7c5d0d9 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java @@ -203,6 +203,8 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe } catch (Exception t) { ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) { + // remove device from cache in case null value cached in the distributed redis. + removeDeviceFromCache(device.getTenantId(), device.getName()); throw new DataValidationException("Device with such name already exists!"); } else { throw t; @@ -281,13 +283,17 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe } deleteEntityRelations(tenantId, deviceId); + removeDeviceFromCache(tenantId, device.getName()); + + deviceDao.removeById(tenantId, deviceId.getId()); + } + + private void removeDeviceFromCache(TenantId tenantId, String name) { List list = new ArrayList<>(); - list.add(device.getTenantId()); - list.add(device.getName()); + list.add(tenantId); + list.add(name); Cache cache = cacheManager.getCache(DEVICE_CACHE); cache.evict(list); - - deviceDao.removeById(tenantId, deviceId.getId()); } @Override