Skip to content

Commit 586d974

Browse files
mp911dechristophstrobl
authored andcommitted
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 Original Pull Request: #2990
1 parent 66c74bc commit 586d974

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/main/java/org/springframework/data/redis/RedisSystemException.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis;
1717

1818
import org.springframework.dao.UncategorizedDataAccessException;
19+
import org.springframework.lang.Nullable;
1920

2021
/**
2122
* 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 {
2829
* @param msg the detail message.
2930
* @param cause the root cause from the data access API in use.
3031
*/
31-
public RedisSystemException(String msg, Throwable cause) {
32+
public RedisSystemException(String msg, @Nullable Throwable cause) {
3233
super(msg, cause);
3334
}
3435

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ private void reset() {
515515
if (this.asyncDedicatedConnection != null) {
516516
try {
517517
if (customizedDatabaseIndex()) {
518-
potentiallySelectDatabase(this.defaultDbIndex);
518+
potentiallySelectDatabase(this.asyncDedicatedConnection, this.defaultDbIndex);
519519
}
520520
this.connectionProvider.release(this.asyncDedicatedConnection);
521521
this.asyncDedicatedConnection = null;
@@ -972,7 +972,7 @@ protected StatefulConnection<byte[], byte[]> doGetAsyncDedicatedConnection() {
972972
StatefulConnection<byte[], byte[]> connection = getConnectionProvider().getConnection(StatefulConnection.class);
973973

974974
if (customizedDatabaseIndex()) {
975-
potentiallySelectDatabase(this.dbIndex);
975+
potentiallySelectDatabase(connection, this.dbIndex);
976976
}
977977

978978
return connection;
@@ -1069,9 +1069,9 @@ private boolean customizedDatabaseIndex() {
10691069
return defaultDbIndex != dbIndex;
10701070
}
10711071

1072-
private void potentiallySelectDatabase(int dbIndex) {
1072+
private static void potentiallySelectDatabase(StatefulConnection<byte[], byte[]> connection, int dbIndex) {
10731073

1074-
if (asyncDedicatedConnection instanceof StatefulRedisConnection<byte[], byte[]> statefulConnection) {
1074+
if (connection instanceof StatefulRedisConnection<byte[], byte[]> statefulConnection) {
10751075
statefulConnection.sync().select(dbIndex);
10761076
}
10771077
}

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTests.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.lettuce.core.*;
2222
import io.lettuce.core.api.StatefulRedisConnection;
2323
import io.lettuce.core.api.async.RedisAsyncCommands;
24+
import io.lettuce.core.api.sync.RedisCommands;
2425
import io.lettuce.core.codec.ByteArrayCodec;
2526
import io.lettuce.core.codec.RedisCodec;
2627
import io.lettuce.core.codec.StringCodec;
@@ -68,6 +69,7 @@ public static class BasicUnitTests extends AbstractConnectionUnitTestBase<RedisA
6869
private RedisClient clientMock;
6970
StatefulRedisConnection<byte[], byte[]> statefulConnectionMock;
7071
RedisAsyncCommands<byte[], byte[]> asyncCommandsMock;
72+
RedisCommands<byte[], byte[]> commandsMock;
7173

7274
@SuppressWarnings({ "unchecked" })
7375
@BeforeEach
@@ -89,8 +91,10 @@ public void setUp() throws InvocationTargetException, IllegalAccessException {
8991
}
9092
return null;
9193
});
94+
commandsMock = Mockito.mock(RedisCommands.class);
9295

9396
when(statefulConnectionMock.async()).thenReturn(asyncCommandsMock);
97+
when(statefulConnectionMock.sync()).thenReturn(commandsMock);
9498
connection = new LettuceConnection(0, clientMock);
9599
}
96100

@@ -155,14 +159,15 @@ void shouldThrowExceptionWhenAccessingRedisSentinelsCommandsWhenNoSentinelsConfi
155159
.isThrownBy(() -> connection.getSentinelConnection());
156160
}
157161

158-
@Test // DATAREDIS-431
162+
@Test // DATAREDIS-431, GH-2984
159163
void dbIndexShouldBeSetWhenObtainingConnection() {
160164

161165
connection = new LettuceConnection(null, 0, clientMock, 0);
162166
connection.select(1);
163167
connection.getNativeConnection();
164168

165169
verify(asyncCommandsMock).dispatch(eq(CommandType.SELECT), any(), any());
170+
verify(commandsMock).select(1);
166171
}
167172

168173
@Test // DATAREDIS-603

0 commit comments

Comments
 (0)