Skip to content

Commit aa4d784

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-714 - Use Jedis API for database selection.
We now rely more on Jedis to select the appropriate Redis database and we no longer select the database when opening/closing a connection. Previously, we always reset the database if a database index greater zero was configured. A properly configured Jedis pool ensures that the appropriate database is selected even if the database was selected during connection interaction. Relying on Jedis reduces the number of issued SELECT commands and improves that way overall performance when executing commands via the Template API. Original Pull Request: #291
1 parent d8ec35f commit aa4d784

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected JedisConnection(Jedis jedis, @Nullable Pool<Jedis> pool, int dbIndex,
120120
// select the db
121121
// if this fail, do manual clean-up before propagating the exception
122122
// as we're inside the constructor
123-
if (dbIndex > 0) {
123+
if (dbIndex != jedis.getDB()) {
124124
try {
125125
select(dbIndex);
126126
} catch (DataAccessException ex) {
@@ -281,19 +281,9 @@ public void close() throws DataAccessException {
281281
if (broken) {
282282
pool.returnBrokenResource(jedis);
283283
} else {
284-
285-
// reset the connection
286-
try {
287-
if (dbIndex > 0) {
288-
jedis.select(0);
289-
}
290-
return;
291-
} catch (Exception ex) {
292-
throw convertJedisAccessException(ex);
293-
} finally {
294-
jedis.close();
295-
}
284+
jedis.close();
296285
}
286+
return;
297287
}
298288
// else close the connection normally (doing the try/catch dance)
299289
Exception exc = null;

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config)
370370

371371
GenericObjectPoolConfig poolConfig = getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig();
372372
return new JedisSentinelPool(config.getMaster().getName(), convertToJedisSentinelSet(config.getSentinels()),
373-
poolConfig, getConnectTimeout(), getReadTimeout(), getPassword(), Protocol.DEFAULT_DATABASE, getClientName());
373+
poolConfig, getConnectTimeout(), getReadTimeout(), getPassword(), getDatabase(), getClientName());
374374
}
375375

376376
/**
@@ -382,7 +382,7 @@ protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config)
382382
protected Pool<Jedis> createRedisPool() {
383383

384384
return new JedisPool(getPoolConfig(), getHostName(), getPort(), getConnectTimeout(), getReadTimeout(),
385-
getPassword(), Protocol.DEFAULT_DATABASE, getClientName(), isUseSsl(),
385+
getPassword(), getDatabase(), getClientName(), isUseSsl(),
386386
clientConfiguration.getSslSocketFactory().orElse(null), //
387387
clientConfiguration.getSslParameters().orElse(null), //
388388
clientConfiguration.getHostnameVerifier().orElse(null));

src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.junit.Test;
3737
import org.junit.runner.RunWith;
3838
import org.springframework.dao.InvalidDataAccessApiUsageException;
39+
import org.springframework.data.redis.RedisConnectionFailureException;
3940
import org.springframework.data.redis.SettingsUtils;
4041
import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests;
4142
import org.springframework.data.redis.connection.ConnectionUtils;
@@ -113,13 +114,18 @@ public void testCreateConnectionWithDb() {
113114
factory2.destroy();
114115
}
115116

116-
@Test(expected = InvalidDataAccessApiUsageException.class)
117+
@Test(expected = RedisConnectionFailureException.class) // DATAREDIS-714
117118
public void testCreateConnectionWithDbFailure() {
119+
118120
JedisConnectionFactory factory2 = new JedisConnectionFactory();
119121
factory2.setDatabase(77);
120122
factory2.afterPropertiesSet();
121-
factory2.getConnection();
122-
factory2.destroy();
123+
124+
try {
125+
factory2.getConnection();
126+
} finally {
127+
factory2.destroy();
128+
}
123129
}
124130

125131
@Test
@@ -381,8 +387,8 @@ public void pExpireShouldSupportExiprationForValuesLargerThanInteger() {
381387
@RequiresRedisSentinel(SentinelsAvailable.ONE_ACTIVE)
382388
public void shouldReturnSentinelCommandsWhenWhenActiveSentinelFound() {
383389

384-
((JedisConnection) byteConnection).setSentinelConfiguration(new RedisSentinelConfiguration().master("mymaster")
385-
.sentinel("127.0.0.1", 26379).sentinel("127.0.0.1", 26380));
390+
((JedisConnection) byteConnection).setSentinelConfiguration(
391+
new RedisSentinelConfiguration().master("mymaster").sentinel("127.0.0.1", 26379).sentinel("127.0.0.1", 26380));
386392
assertThat(connection.getSentinelConnection(), notNullValue());
387393
}
388394

0 commit comments

Comments
 (0)