From 8fba15bc33765c753eb279bc4e4c55106608aa8a Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Sat, 7 Oct 2023 13:51:14 +0300 Subject: [PATCH] Fixed - RedissonClient.getLiveObjectService() method causes an attempt to connect to Redis if lazyInitialization = true. #5338 --- .../org/redisson/RedissonLiveObjectService.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java b/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java index b72b9eeeb..09ce186d5 100644 --- a/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java +++ b/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java @@ -71,7 +71,9 @@ public class RedissonLiveObjectService implements RLiveObjectService { this.classCache = classCache; this.commandExecutor = commandExecutor; this.seachEngine = new LiveObjectSearch(commandExecutor); + } + private void addExpireListener(CommandAsyncExecutor commandExecutor) { if (commandExecutor.getServiceManager().getLiveObjectLatch().compareAndSet(false, true)) { String pp = "__keyspace@" + commandExecutor.getServiceManager().getConfig().getDatabase() + "__:redisson_live_object:*"; String prefix = pp.replace(":redisson_live_object:*", ""); @@ -150,6 +152,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { @Override public T get(Class entityClass, Object id) { + addExpireListener(commandExecutor); T proxied = createLiveObject(entityClass, id); if (asLiveObject(proxied).isExists()) { return proxied; @@ -159,21 +162,24 @@ public class RedissonLiveObjectService implements RLiveObjectService { @Override public Collection find(Class entityClass, Condition condition) { + addExpireListener(commandExecutor); Set ids = seachEngine.find(entityClass, condition); return ids.stream() - .map(id -> createLiveObject(entityClass, id)) - .collect(Collectors.toList()); + .map(id -> createLiveObject(entityClass, id)) + .collect(Collectors.toList()); } @Override public long count(Class entityClass, Condition condition) { + addExpireListener(commandExecutor); Set ids = seachEngine.find(entityClass, condition); return ids.size(); } @Override public T attach(T detachedObject) { + addExpireListener(commandExecutor); validateDetached(detachedObject); Class entityClass = (Class) detachedObject.getClass(); String idFieldName = getRIdFieldName(detachedObject.getClass()); @@ -211,6 +217,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { } public List persist(RCascadeType type, T... detachedObjects) { + addExpireListener(commandExecutor); CommandBatchService batchService = new CommandBatchService(commandExecutor); Map, Class> classCache = new HashMap<>(); @@ -286,6 +293,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { private T persist(T detachedObject, Map alreadyPersisted, RCascadeType type) { + addExpireListener(commandExecutor); validateDetached(detachedObject); Object id = getId(detachedObject); @@ -411,6 +419,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { @Override public T detach(T attachedObject) { + addExpireListener(commandExecutor); Map alreadyDetached = new HashMap(); return detach(attachedObject, alreadyDetached); } @@ -544,6 +553,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { @Override public void delete(T attachedObject) { + addExpireListener(commandExecutor); Set deleted = new HashSet(); delete(attachedObject, deleted); } @@ -600,6 +610,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { @Override public long delete(Class entityClass, Object... ids) { + addExpireListener(commandExecutor); CommandBatchService ce = new CommandBatchService(commandExecutor); FieldList fields = Introspectior.getFieldsWithAnnotation(entityClass, RIndex.class); Set fieldNames = fields.stream().map(f -> f.getName()).collect(Collectors.toSet()); @@ -695,6 +706,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { @Override public Iterable findIds(Class entityClass, int count) { + addExpireListener(commandExecutor); NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass); String pattern = namingScheme.getNamePattern(entityClass); RedissonKeys keys = new RedissonKeys(commandExecutor); @@ -730,6 +742,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { @Override public boolean isExists(T instance) { + addExpireListener(commandExecutor); return instance instanceof RLiveObject && asLiveObject(instance).isExists(); }