From e00399f081c9f8bf26d8416ac0e6599f4457c1ec Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 11 Sep 2024 14:10:41 +0200 Subject: [PATCH 1/2] Prepare issue branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 09bebeb5bd..b06ec4a29e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-redis - 3.4.0-SNAPSHOT + 3.4.0-2984-SNAPSHOT Spring Data Redis Spring Data module for Redis From cf30543164055dd4b3a8a094d525d6c43b8ad998 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 11 Sep 2024 14:10:50 +0200 Subject: [PATCH 2/2] Fix database selection on dedicated connection. We now select the database on the dedicated connection. Previously, this call never happened on the dedicated connection and the only way a database could be selected is through the ConnectionFactory configuration. Closes #2984 --- .../springframework/data/redis/RedisSystemException.java | 3 ++- .../data/redis/connection/lettuce/LettuceConnection.java | 8 ++++---- .../connection/lettuce/LettuceConnectionUnitTests.java | 7 ++++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/RedisSystemException.java b/src/main/java/org/springframework/data/redis/RedisSystemException.java index 50d2218dd3..0767a37698 100644 --- a/src/main/java/org/springframework/data/redis/RedisSystemException.java +++ b/src/main/java/org/springframework/data/redis/RedisSystemException.java @@ -16,6 +16,7 @@ package org.springframework.data.redis; import org.springframework.dao.UncategorizedDataAccessException; +import org.springframework.lang.Nullable; /** * Exception thrown when we can't classify a Redis exception into one of Spring generic data access exceptions. @@ -28,7 +29,7 @@ public class RedisSystemException extends UncategorizedDataAccessException { * @param msg the detail message. * @param cause the root cause from the data access API in use. */ - public RedisSystemException(String msg, Throwable cause) { + public RedisSystemException(String msg, @Nullable Throwable cause) { super(msg, cause); } diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java index 0016e73a4c..2a7a79183f 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java @@ -512,7 +512,7 @@ private void reset() { if (this.asyncDedicatedConnection != null) { try { if (customizedDatabaseIndex()) { - potentiallySelectDatabase(this.defaultDbIndex); + potentiallySelectDatabase(this.asyncDedicatedConnection, this.defaultDbIndex); } this.connectionProvider.release(this.asyncDedicatedConnection); this.asyncDedicatedConnection = null; @@ -965,7 +965,7 @@ protected StatefulConnection doGetAsyncDedicatedConnection() { StatefulConnection connection = getConnectionProvider().getConnection(StatefulConnection.class); if (customizedDatabaseIndex()) { - potentiallySelectDatabase(this.dbIndex); + potentiallySelectDatabase(connection, this.dbIndex); } return connection; @@ -1062,9 +1062,9 @@ private boolean customizedDatabaseIndex() { return defaultDbIndex != dbIndex; } - private void potentiallySelectDatabase(int dbIndex) { + private static void potentiallySelectDatabase(StatefulConnection connection, int dbIndex) { - if (asyncDedicatedConnection instanceof StatefulRedisConnection statefulConnection) { + if (connection instanceof StatefulRedisConnection statefulConnection) { statefulConnection.sync().select(dbIndex); } } diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTests.java index 0a6c19a166..e56af1cb3b 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTests.java @@ -21,6 +21,7 @@ import io.lettuce.core.*; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.api.async.RedisAsyncCommands; +import io.lettuce.core.api.sync.RedisCommands; import io.lettuce.core.codec.ByteArrayCodec; import io.lettuce.core.codec.RedisCodec; import io.lettuce.core.codec.StringCodec; @@ -68,6 +69,7 @@ public static class BasicUnitTests extends AbstractConnectionUnitTestBase statefulConnectionMock; RedisAsyncCommands asyncCommandsMock; + RedisCommands commandsMock; @SuppressWarnings({ "unchecked" }) @BeforeEach @@ -89,8 +91,10 @@ public void setUp() throws InvocationTargetException, IllegalAccessException { } return null; }); + commandsMock = Mockito.mock(RedisCommands.class); when(statefulConnectionMock.async()).thenReturn(asyncCommandsMock); + when(statefulConnectionMock.sync()).thenReturn(commandsMock); connection = new LettuceConnection(0, clientMock); } @@ -155,7 +159,7 @@ void shouldThrowExceptionWhenAccessingRedisSentinelsCommandsWhenNoSentinelsConfi .isThrownBy(() -> connection.getSentinelConnection()); } - @Test // DATAREDIS-431 + @Test // DATAREDIS-431, GH-2984 void dbIndexShouldBeSetWhenObtainingConnection() { connection = new LettuceConnection(null, 0, clientMock, 0); @@ -163,6 +167,7 @@ void dbIndexShouldBeSetWhenObtainingConnection() { connection.getNativeConnection(); verify(asyncCommandsMock).dispatch(eq(CommandType.SELECT), any(), any()); + verify(commandsMock).select(1); } @Test // DATAREDIS-603