Skip to content

Commit bcc91f6

Browse files
committed
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: spring-projects#496.
1 parent 0fdea07 commit bcc91f6

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
* @author David Liu
8585
* @author Mark Paluch
8686
* @author Ninad Divadkar
87+
* @author Tamil Selvan
8788
*/
8889
public class LettuceConnection extends AbstractRedisConnection {
8990

@@ -451,6 +452,9 @@ public void close() throws DataAccessException {
451452

452453
if (asyncDedicatedConn != null) {
453454
try {
455+
if (customizedDatabaseIndex()) {
456+
potentiallySelectDatabase(defaultDbIndex);
457+
}
454458
connectionProvider.release(asyncDedicatedConn);
455459
} catch (RuntimeException ex) {
456460
throw convertLettuceAccessException(ex);
@@ -946,12 +950,7 @@ private RedisAsyncCommands<byte[], byte[]> getAsyncDedicatedRedisCommands() {
946950
protected RedisClusterAsyncCommands<byte[], byte[]> getAsyncDedicatedConnection() {
947951

948952
if (asyncDedicatedConn == null) {
949-
950953
asyncDedicatedConn = doGetAsyncDedicatedConnection();
951-
952-
if (asyncDedicatedConn instanceof StatefulRedisConnection) {
953-
((StatefulRedisConnection<byte[], byte[]>) asyncDedicatedConn).sync().select(dbIndex);
954-
}
955954
}
956955

957956
if (asyncDedicatedConn instanceof StatefulRedisConnection) {
@@ -965,6 +964,16 @@ protected RedisClusterAsyncCommands<byte[], byte[]> getAsyncDedicatedConnection(
965964
String.format("%s is not a supported connection type.", asyncDedicatedConn.getClass().getName()));
966965
}
967966

967+
private boolean customizedDatabaseIndex() {
968+
return defaultDbIndex != dbIndex;
969+
}
970+
971+
private void potentiallySelectDatabase(int dbIndex) {
972+
if (asyncDedicatedConn instanceof StatefulRedisConnection) {
973+
((StatefulRedisConnection<byte[], byte[]>) asyncDedicatedConn).sync().select(dbIndex);
974+
}
975+
}
976+
968977
@SuppressWarnings("unchecked")
969978
private RedisCommands<byte[], byte[]> getDedicatedRedisCommands() {
970979
return (RedisCommands) getDedicatedConnection();
@@ -973,15 +982,7 @@ private RedisCommands<byte[], byte[]> getDedicatedRedisCommands() {
973982
RedisClusterCommands<byte[], byte[]> getDedicatedConnection() {
974983

975984
if (asyncDedicatedConn == null) {
976-
977985
asyncDedicatedConn = doGetAsyncDedicatedConnection();
978-
979-
if (asyncDedicatedConn instanceof StatefulRedisConnection && dbIndex > 0) {
980-
((StatefulRedisConnection<byte[], byte[]>) asyncDedicatedConn).sync().select(dbIndex);
981-
}
982-
else if (asyncDedicatedConn instanceof StatefulRedisConnection) {
983-
((StatefulRedisConnection<byte[], byte[]>) asyncDedicatedConn).sync();
984-
}
985986
}
986987

987988
if (asyncDedicatedConn instanceof StatefulRedisConnection) {
@@ -997,7 +998,14 @@ else if (asyncDedicatedConn instanceof StatefulRedisConnection) {
997998

998999
@SuppressWarnings("unchecked")
9991000
protected StatefulConnection<byte[], byte[]> doGetAsyncDedicatedConnection() {
1000-
return connectionProvider.getConnection(StatefulConnection.class);
1001+
1002+
StatefulConnection connection = connectionProvider.getConnection(StatefulConnection.class);
1003+
1004+
if (customizedDatabaseIndex()) {
1005+
potentiallySelectDatabase(dbIndex);
1006+
}
1007+
1008+
return connection;
10011009
}
10021010

10031011
io.lettuce.core.ScanCursor getScanCursor(long cursorId) {

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Set;
2727
import java.util.concurrent.atomic.AtomicBoolean;
2828

29+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
2930
import org.junit.Rule;
3031
import org.junit.Test;
3132
import org.junit.runner.RunWith;
@@ -213,18 +214,33 @@ public void testCloseReturnBrokenResourceToPool() {
213214
pool.destroy();
214215
}
215216

216-
@Test
217+
@Test // DATAREDIS-1062
217218
public void testSelectNotShared() {
218219
DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
220+
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
221+
config.setMaxTotal(1);
222+
config.setMaxIdle(1);
223+
pool.setPoolConfig(config);
219224
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
220225
pool.afterPropertiesSet();
221226
LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
227+
factory2.setDatabase(0);
222228
factory2.setShutdownTimeout(0);
223229
factory2.setShareNativeConnection(false);
224230
factory2.afterPropertiesSet();
225231
RedisConnection connection = factory2.getConnection();
232+
226233
connection.select(2);
234+
connection.rPush("key".getBytes(), "value1".getBytes(), "value2".getBytes());
235+
List<byte[]> bytes = connection.bLPop(1, "key".getBytes());
236+
assertThat(bytes).hasSize(2);
227237
connection.close();
238+
239+
connection = factory2.getConnection();
240+
assertThat(connection.lLen("key".getBytes())).isEqualTo(0);
241+
connection.select(2);
242+
assertThat(connection.lLen("key".getBytes())).isEqualTo(1);
243+
228244
factory2.destroy();
229245
pool.destroy();
230246
}

0 commit comments

Comments
 (0)