From 64d1ab55d46c64984da8fab098e4f66ea3818b72 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Sun, 24 Mar 2024 09:02:41 +0300 Subject: [PATCH] Fixed - ConcurrentModificationException thrown on RedissonSession save method if readMode = MEMORY and updateMode = AFTER_REQUEST #5677 --- .../tomcat/AttributesPutAllMessage.java | 32 +++++++++++-------- .../org/redisson/tomcat/RedissonSession.java | 2 +- .../tomcat/AttributesPutAllMessage.java | 32 +++++++++++-------- .../org/redisson/tomcat/RedissonSession.java | 2 +- .../tomcat/AttributesPutAllMessage.java | 32 +++++++++++-------- .../org/redisson/tomcat/RedissonSession.java | 2 +- .../tomcat/AttributesPutAllMessage.java | 32 +++++++++++-------- .../org/redisson/tomcat/RedissonSession.java | 2 +- 8 files changed, 76 insertions(+), 60 deletions(-) diff --git a/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java b/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java index 07c906976..4733c8c3b 100644 --- a/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java +++ b/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java @@ -37,26 +37,30 @@ public class AttributesPutAllMessage extends AttributeMessage { public AttributesPutAllMessage() { } - public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs, Encoder encoder) throws Exception { - super(nodeId, sessionId); + public AttributesPutAllMessage(RedissonSessionManager redissonSessionManager, String sessionId, Map attrs, Encoder encoder) throws Exception { + super(redissonSessionManager.getNodeId(), sessionId); if (attrs != null) { this.attrs = new HashMap<>(); for (Entry entry: attrs.entrySet()) { Object value = entry.getValue(); - try { - if (value instanceof Collection) { - Collection newInstance = (Collection) value.getClass().getDeclaredConstructor().newInstance(); - newInstance.addAll((Collection) value); - value = newInstance; + if (redissonSessionManager.getReadMode() + .equals(RedissonSessionManager.ReadMode.MEMORY.toString())) { + try { + if (value instanceof Collection) { + Collection newInstance = (Collection) value.getClass().getDeclaredConstructor().newInstance(); + newInstance.addAll((Collection) value); + value = newInstance; + } + if (value instanceof Map) { + Map newInstance = (Map) value.getClass().getDeclaredConstructor().newInstance(); + newInstance.putAll((Map) value); + value = newInstance; + } + } catch (Exception e) { + // can't be copied } - if (value instanceof Map) { - Map newInstance = (Map) value.getClass().getDeclaredConstructor().newInstance(); - newInstance.putAll((Map) value); - value = newInstance; - } - } catch (Exception e) { - // can't be copied } + this.attrs.put(entry.getKey(), toByteArray(encoder, value)); } } else { diff --git a/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/RedissonSession.java index f14149b9e..aa2a67bad 100644 --- a/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -236,7 +236,7 @@ public class RedissonSession extends StandardSession { protected AttributesPutAllMessage createPutAllMessage(Map newMap) { try { - return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), newMap, this.map.getCodec().getMapValueEncoder()); + return new AttributesPutAllMessage(redissonManager, getId(), newMap, this.map.getCodec().getMapValueEncoder()); } catch (Exception e) { throw new IllegalStateException(e); } diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java index a66c811dc..e0e2420f0 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java @@ -36,26 +36,30 @@ public class AttributesPutAllMessage extends AttributeMessage { public AttributesPutAllMessage() { } - public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs, Encoder encoder) throws Exception { - super(nodeId, sessionId); + public AttributesPutAllMessage(RedissonSessionManager redissonSessionManager, String sessionId, Map attrs, Encoder encoder) throws Exception { + super(redissonSessionManager.getNodeId(), sessionId); if (attrs != null) { this.attrs = new HashMap<>(); for (Entry entry: attrs.entrySet()) { Object value = entry.getValue(); - try { - if (value instanceof Collection) { - Collection newInstance = (Collection) value.getClass().getDeclaredConstructor().newInstance(); - newInstance.addAll((Collection) value); - value = newInstance; + if (redissonSessionManager.getReadMode() + .equals(RedissonSessionManager.ReadMode.MEMORY.toString())) { + try { + if (value instanceof Collection) { + Collection newInstance = (Collection) value.getClass().getDeclaredConstructor().newInstance(); + newInstance.addAll((Collection) value); + value = newInstance; + } + if (value instanceof Map) { + Map newInstance = (Map) value.getClass().getDeclaredConstructor().newInstance(); + newInstance.putAll((Map) value); + value = newInstance; + } + } catch (Exception e) { + // can't be copied } - if (value instanceof Map) { - Map newInstance = (Map) value.getClass().getDeclaredConstructor().newInstance(); - newInstance.putAll((Map) value); - value = newInstance; - } - } catch (Exception e) { - // can't be copied } + this.attrs.put(entry.getKey(), toByteArray(encoder, value)); } } else { diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java index ac558e006..9904ad13e 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -230,7 +230,7 @@ public class RedissonSession extends StandardSession { protected AttributesPutAllMessage createPutAllMessage(Map newMap) { try { - return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), newMap, this.map.getCodec().getMapValueEncoder()); + return new AttributesPutAllMessage(redissonManager, getId(), newMap, this.map.getCodec().getMapValueEncoder()); } catch (Exception e) { throw new IllegalStateException(e); } diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java index 83fdf3c48..1ed2119a8 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java @@ -36,26 +36,30 @@ public class AttributesPutAllMessage extends AttributeMessage { public AttributesPutAllMessage() { } - public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs, Encoder encoder) throws Exception { - super(nodeId, sessionId); + public AttributesPutAllMessage(RedissonSessionManager redissonSessionManager, String sessionId, Map attrs, Encoder encoder) throws Exception { + super(redissonSessionManager.getNodeId(), sessionId); if (attrs != null) { this.attrs = new HashMap<>(); for (Entry entry: attrs.entrySet()) { Object value = entry.getValue(); - try { - if (value instanceof Collection) { - Collection newInstance = (Collection) value.getClass().getDeclaredConstructor().newInstance(); - newInstance.addAll((Collection) value); - value = newInstance; + if (redissonSessionManager.getReadMode() + .equals(RedissonSessionManager.ReadMode.MEMORY.toString())) { + try { + if (value instanceof Collection) { + Collection newInstance = (Collection) value.getClass().getDeclaredConstructor().newInstance(); + newInstance.addAll((Collection) value); + value = newInstance; + } + if (value instanceof Map) { + Map newInstance = (Map) value.getClass().getDeclaredConstructor().newInstance(); + newInstance.putAll((Map) value); + value = newInstance; + } + } catch (Exception e) { + // can't be copied } - if (value instanceof Map) { - Map newInstance = (Map) value.getClass().getDeclaredConstructor().newInstance(); - newInstance.putAll((Map) value); - value = newInstance; - } - } catch (Exception e) { - // can't be copied } + this.attrs.put(entry.getKey(), toByteArray(encoder, value)); } } else { diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java index c40e6ac43..ed936521c 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -231,7 +231,7 @@ public class RedissonSession extends StandardSession { protected AttributesPutAllMessage createPutAllMessage(Map newMap) { try { - return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), newMap, this.map.getCodec().getMapValueEncoder()); + return new AttributesPutAllMessage(redissonManager, getId(), newMap, this.map.getCodec().getMapValueEncoder()); } catch (Exception e) { throw new IllegalStateException(e); } diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java index 83fdf3c48..1ed2119a8 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributesPutAllMessage.java @@ -36,26 +36,30 @@ public class AttributesPutAllMessage extends AttributeMessage { public AttributesPutAllMessage() { } - public AttributesPutAllMessage(String nodeId, String sessionId, Map attrs, Encoder encoder) throws Exception { - super(nodeId, sessionId); + public AttributesPutAllMessage(RedissonSessionManager redissonSessionManager, String sessionId, Map attrs, Encoder encoder) throws Exception { + super(redissonSessionManager.getNodeId(), sessionId); if (attrs != null) { this.attrs = new HashMap<>(); for (Entry entry: attrs.entrySet()) { Object value = entry.getValue(); - try { - if (value instanceof Collection) { - Collection newInstance = (Collection) value.getClass().getDeclaredConstructor().newInstance(); - newInstance.addAll((Collection) value); - value = newInstance; + if (redissonSessionManager.getReadMode() + .equals(RedissonSessionManager.ReadMode.MEMORY.toString())) { + try { + if (value instanceof Collection) { + Collection newInstance = (Collection) value.getClass().getDeclaredConstructor().newInstance(); + newInstance.addAll((Collection) value); + value = newInstance; + } + if (value instanceof Map) { + Map newInstance = (Map) value.getClass().getDeclaredConstructor().newInstance(); + newInstance.putAll((Map) value); + value = newInstance; + } + } catch (Exception e) { + // can't be copied } - if (value instanceof Map) { - Map newInstance = (Map) value.getClass().getDeclaredConstructor().newInstance(); - newInstance.putAll((Map) value); - value = newInstance; - } - } catch (Exception e) { - // can't be copied } + this.attrs.put(entry.getKey(), toByteArray(encoder, value)); } } else { diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java index c40e6ac43..ed936521c 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -231,7 +231,7 @@ public class RedissonSession extends StandardSession { protected AttributesPutAllMessage createPutAllMessage(Map newMap) { try { - return new AttributesPutAllMessage(redissonManager.getNodeId(), getId(), newMap, this.map.getCodec().getMapValueEncoder()); + return new AttributesPutAllMessage(redissonManager, getId(), newMap, this.map.getCodec().getMapValueEncoder()); } catch (Exception e) { throw new IllegalStateException(e); }