Skip to content

Commit fd23412

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 02dfe5c commit fd23412

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
@@ -512,7 +512,7 @@ private void reset() {
512512
if (this.asyncDedicatedConnection != null) {
513513
try {
514514
if (customizedDatabaseIndex()) {
515-
potentiallySelectDatabase(this.defaultDbIndex);
515+
potentiallySelectDatabase(this.asyncDedicatedConnection, this.defaultDbIndex);
516516
}
517517
this.connectionProvider.release(this.asyncDedicatedConnection);
518518
this.asyncDedicatedConnection = null;
@@ -965,7 +965,7 @@ protected StatefulConnection<byte[], byte[]> doGetAsyncDedicatedConnection() {
965965
StatefulConnection<byte[], byte[]> connection = getConnectionProvider().getConnection(StatefulConnection.class);
966966

967967
if (customizedDatabaseIndex()) {
968-
potentiallySelectDatabase(this.dbIndex);
968+
potentiallySelectDatabase(connection, this.dbIndex);
969969
}
970970

971971
return connection;
@@ -1062,9 +1062,9 @@ private boolean customizedDatabaseIndex() {
10621062
return defaultDbIndex != dbIndex;
10631063
}
10641064

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

1067-
if (asyncDedicatedConnection instanceof StatefulRedisConnection<byte[], byte[]> statefulConnection) {
1067+
if (connection instanceof StatefulRedisConnection<byte[], byte[]> statefulConnection) {
10681068
statefulConnection.sync().select(dbIndex);
10691069
}
10701070
}

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)