Skip to content

Commit

Permalink
Fix database selection on dedicated connection.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mp911de authored and christophstrobl committed Sep 12, 2024
1 parent 02dfe5c commit fd23412
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -965,7 +965,7 @@ protected StatefulConnection<byte[], byte[]> doGetAsyncDedicatedConnection() {
StatefulConnection<byte[], byte[]> connection = getConnectionProvider().getConnection(StatefulConnection.class);

if (customizedDatabaseIndex()) {
potentiallySelectDatabase(this.dbIndex);
potentiallySelectDatabase(connection, this.dbIndex);
}

return connection;
Expand Down Expand Up @@ -1062,9 +1062,9 @@ private boolean customizedDatabaseIndex() {
return defaultDbIndex != dbIndex;
}

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

if (asyncDedicatedConnection instanceof StatefulRedisConnection<byte[], byte[]> statefulConnection) {
if (connection instanceof StatefulRedisConnection<byte[], byte[]> statefulConnection) {
statefulConnection.sync().select(dbIndex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,7 @@ public static class BasicUnitTests extends AbstractConnectionUnitTestBase<RedisA
private RedisClient clientMock;
StatefulRedisConnection<byte[], byte[]> statefulConnectionMock;
RedisAsyncCommands<byte[], byte[]> asyncCommandsMock;
RedisCommands<byte[], byte[]> commandsMock;

@SuppressWarnings({ "unchecked" })
@BeforeEach
Expand All @@ -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);
}

Expand Down Expand Up @@ -155,14 +159,15 @@ void shouldThrowExceptionWhenAccessingRedisSentinelsCommandsWhenNoSentinelsConfi
.isThrownBy(() -> connection.getSentinelConnection());
}

@Test // DATAREDIS-431
@Test // DATAREDIS-431, GH-2984
void dbIndexShouldBeSetWhenObtainingConnection() {

connection = new LettuceConnection(null, 0, clientMock, 0);
connection.select(1);
connection.getNativeConnection();

verify(asyncCommandsMock).dispatch(eq(CommandType.SELECT), any(), any());
verify(commandsMock).select(1);
}

@Test // DATAREDIS-603
Expand Down

0 comments on commit fd23412

Please sign in to comment.