DATAREDIS-973 - Fix database selection using Lettuce.
We now correctly set the requested database using RedisURI so the driver performs the database selection. We no longer need to call select ourselves. For shared connections (pooled and single shared connection) we now no longer call select. This can leave connections with a different database associated. Original Pull Request: #451
This commit is contained in:
committed by
Christoph Strobl
parent
3496cc8108
commit
3e9cedfdcb
@@ -437,7 +437,7 @@ public class LettuceConnectionFactory
|
||||
synchronized (this.connectionMonitor) {
|
||||
|
||||
if (this.connection == null) {
|
||||
this.connection = new SharedConnection<>(connectionProvider, false);
|
||||
this.connection = new SharedConnection<>(connectionProvider);
|
||||
}
|
||||
|
||||
return this.connection;
|
||||
@@ -449,7 +449,7 @@ public class LettuceConnectionFactory
|
||||
synchronized (this.connectionMonitor) {
|
||||
|
||||
if (this.reactiveConnection == null) {
|
||||
this.reactiveConnection = new SharedConnection<>(reactiveConnectionProvider, true);
|
||||
this.reactiveConnection = new SharedConnection<>(reactiveConnectionProvider);
|
||||
}
|
||||
|
||||
return this.reactiveConnection;
|
||||
@@ -1013,6 +1013,7 @@ public class LettuceConnectionFactory
|
||||
getRedisPassword().toOptional().ifPresent(builder::withPassword);
|
||||
clientConfiguration.getClientName().ifPresent(builder::withClientName);
|
||||
|
||||
builder.withDatabase(getDatabase());
|
||||
builder.withSsl(clientConfiguration.isUseSsl());
|
||||
builder.withVerifyPeer(clientConfiguration.isVerifyPeer());
|
||||
builder.withStartTls(clientConfiguration.isStartTls());
|
||||
@@ -1026,6 +1027,7 @@ public class LettuceConnectionFactory
|
||||
RedisURI.Builder builder = RedisURI.Builder.socket(socketPath);
|
||||
|
||||
getRedisPassword().toOptional().ifPresent(builder::withPassword);
|
||||
builder.withDatabase(getDatabase());
|
||||
builder.withTimeout(clientConfiguration.getCommandTimeout());
|
||||
|
||||
return builder.build();
|
||||
@@ -1062,7 +1064,6 @@ public class LettuceConnectionFactory
|
||||
class SharedConnection<E> {
|
||||
|
||||
private final LettuceConnectionProvider connectionProvider;
|
||||
private final boolean shareNativeClusterConnection;
|
||||
|
||||
/** Synchronization monitor for the shared Connection */
|
||||
private final Object connectionMonitor = new Object();
|
||||
@@ -1100,13 +1101,7 @@ public class LettuceConnectionFactory
|
||||
private StatefulConnection<E, E> getNativeConnection() {
|
||||
|
||||
try {
|
||||
|
||||
StatefulConnection<E, E> connection = connectionProvider.getConnection(StatefulConnection.class);
|
||||
if (connection instanceof StatefulRedisConnection && getDatabase() > 0) {
|
||||
((StatefulRedisConnection) connection).sync().select(getDatabase());
|
||||
}
|
||||
|
||||
return connection;
|
||||
return connectionProvider.getConnection(StatefulConnection.class);
|
||||
} catch (RedisException e) {
|
||||
throw new RedisConnectionFailureException("Unable to connect to Redis", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user