From bf81096ad353ec3272dbebbb83bf69a0f380c3d7 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Thu, 29 Jan 2026 22:26:30 +0000 Subject: [PATCH] Fixup JdbcIndexedSessionRepository SessionResultSetExtractor#extractData should not return a list containing multiple entries for the same JdbcSession This is confusing when debugging the code. Add a warning when >1 session is found for the same ID. This is an indicator that data is corrupt; most of the schemas prevent at the database level by adding a unique constraint to the primary ID column. If the code encounters this scenario; it should fail fast, however this might be a breaking change. A WARN level logging helps developers initially spot this situation and remedy. Signed-off-by: Martin Ashby --- .../session/jdbc/JdbcIndexedSessionRepository.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java index 98b46de85..21b57573c 100644 --- a/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java +++ b/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcIndexedSessionRepository.java @@ -486,6 +486,9 @@ public JdbcSession findById(final String id) { if (sessions.isEmpty()) { return null; } + if (sessions.size() > 1) { + logger.warn("Found " + sessions.size() + " sessions for id " + id + ". Returning the first one."); + } return sessions.get(0); }); @@ -982,13 +985,13 @@ public List extractData(ResultSet rs) throws SQLException, DataAcce delegate.setLastAccessedTime(Instant.ofEpochMilli(rs.getLong("LAST_ACCESS_TIME"))); delegate.setMaxInactiveInterval(Duration.ofSeconds(rs.getInt("MAX_INACTIVE_INTERVAL"))); session = new JdbcSession(delegate, primaryKey, false); + sessions.add(session); } String attributeName = rs.getString("ATTRIBUTE_NAME"); if (attributeName != null) { byte[] bytes = getLobHandler().getBlobAsBytes(rs, "ATTRIBUTE_BYTES"); session.delegate.setAttribute(attributeName, lazily(() -> deserialize(bytes))); } - sessions.add(session); } return sessions; }