DATAREDIS-1062 - Polishing.

We now select the database on dedicated connection acquisition only if the current database is different from the default database. Database connections are expected to use the default database index when they are acquired so the database index may be changed for the duration when a connection is in use.
We also reset dedicated connections to use the default database index on connection cleanup to bring the connection back into its initial state.

Add author tag and tests.

Original pull request: #496.
This commit is contained in:
Mark Paluch
2019-11-22 12:59:39 +01:00
parent 0fdea07294
commit bcc91f6b43
2 changed files with 39 additions and 15 deletions

View File

@@ -84,6 +84,7 @@ import org.springframework.util.ObjectUtils;
* @author David Liu
* @author Mark Paluch
* @author Ninad Divadkar
* @author Tamil Selvan
*/
public class LettuceConnection extends AbstractRedisConnection {
@@ -451,6 +452,9 @@ public class LettuceConnection extends AbstractRedisConnection {
if (asyncDedicatedConn != null) {
try {
if (customizedDatabaseIndex()) {
potentiallySelectDatabase(defaultDbIndex);
}
connectionProvider.release(asyncDedicatedConn);
} catch (RuntimeException ex) {
throw convertLettuceAccessException(ex);
@@ -946,12 +950,7 @@ public class LettuceConnection extends AbstractRedisConnection {
protected RedisClusterAsyncCommands<byte[], byte[]> getAsyncDedicatedConnection() {
if (asyncDedicatedConn == null) {
asyncDedicatedConn = doGetAsyncDedicatedConnection();
if (asyncDedicatedConn instanceof StatefulRedisConnection) {
((StatefulRedisConnection<byte[], byte[]>) asyncDedicatedConn).sync().select(dbIndex);
}
}
if (asyncDedicatedConn instanceof StatefulRedisConnection) {
@@ -965,6 +964,16 @@ public class LettuceConnection extends AbstractRedisConnection {
String.format("%s is not a supported connection type.", asyncDedicatedConn.getClass().getName()));
}
private boolean customizedDatabaseIndex() {
return defaultDbIndex != dbIndex;
}
private void potentiallySelectDatabase(int dbIndex) {
if (asyncDedicatedConn instanceof StatefulRedisConnection) {
((StatefulRedisConnection<byte[], byte[]>) asyncDedicatedConn).sync().select(dbIndex);
}
}
@SuppressWarnings("unchecked")
private RedisCommands<byte[], byte[]> getDedicatedRedisCommands() {
return (RedisCommands) getDedicatedConnection();
@@ -973,15 +982,7 @@ public class LettuceConnection extends AbstractRedisConnection {
RedisClusterCommands<byte[], byte[]> getDedicatedConnection() {
if (asyncDedicatedConn == null) {
asyncDedicatedConn = doGetAsyncDedicatedConnection();
if (asyncDedicatedConn instanceof StatefulRedisConnection && dbIndex > 0) {
((StatefulRedisConnection<byte[], byte[]>) asyncDedicatedConn).sync().select(dbIndex);
}
else if (asyncDedicatedConn instanceof StatefulRedisConnection) {
((StatefulRedisConnection<byte[], byte[]>) asyncDedicatedConn).sync();
}
}
if (asyncDedicatedConn instanceof StatefulRedisConnection) {
@@ -997,7 +998,14 @@ public class LettuceConnection extends AbstractRedisConnection {
@SuppressWarnings("unchecked")
protected StatefulConnection<byte[], byte[]> doGetAsyncDedicatedConnection() {
return connectionProvider.getConnection(StatefulConnection.class);
StatefulConnection connection = connectionProvider.getConnection(StatefulConnection.class);
if (customizedDatabaseIndex()) {
potentiallySelectDatabase(dbIndex);
}
return connection;
}
io.lettuce.core.ScanCursor getScanCursor(long cursorId) {