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
This commit is contained in:
Mark Paluch
2017-11-02 15:00:38 +01:00
committed by Christoph Strobl
parent d8ec35f48b
commit aa4d784df0
3 changed files with 16 additions and 20 deletions

View File

@@ -120,7 +120,7 @@ public class JedisConnection extends AbstractRedisConnection {
// select the db
// if this fail, do manual clean-up before propagating the exception
// as we're inside the constructor
if (dbIndex > 0) {
if (dbIndex != jedis.getDB()) {
try {
select(dbIndex);
} catch (DataAccessException ex) {
@@ -281,19 +281,9 @@ public class JedisConnection extends AbstractRedisConnection {
if (broken) {
pool.returnBrokenResource(jedis);
} else {
// reset the connection
try {
if (dbIndex > 0) {
jedis.select(0);
}
return;
} catch (Exception ex) {
throw convertJedisAccessException(ex);
} finally {
jedis.close();
}
jedis.close();
}
return;
}
// else close the connection normally (doing the try/catch dance)
Exception exc = null;

View File

@@ -370,7 +370,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
GenericObjectPoolConfig poolConfig = getPoolConfig() != null ? getPoolConfig() : new JedisPoolConfig();
return new JedisSentinelPool(config.getMaster().getName(), convertToJedisSentinelSet(config.getSentinels()),
poolConfig, getConnectTimeout(), getReadTimeout(), getPassword(), Protocol.DEFAULT_DATABASE, getClientName());
poolConfig, getConnectTimeout(), getReadTimeout(), getPassword(), getDatabase(), getClientName());
}
/**
@@ -382,7 +382,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
protected Pool<Jedis> createRedisPool() {
return new JedisPool(getPoolConfig(), getHostName(), getPort(), getConnectTimeout(), getReadTimeout(),
getPassword(), Protocol.DEFAULT_DATABASE, getClientName(), isUseSsl(),
getPassword(), getDatabase(), getClientName(), isUseSsl(),
clientConfiguration.getSslSocketFactory().orElse(null), //
clientConfiguration.getSslParameters().orElse(null), //
clientConfiguration.getHostnameVerifier().orElse(null));