Skip to content

Fix database selection on dedicated connection #2990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-2984-SNAPSHOT</version>

<name>Spring Data Redis</name>
<description>Spring Data module for Redis</description>
Expand Down
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