Skip to content

Commit 87dd0f2

Browse files
committed
Fixed issue with database selection causing the tests to fail
Fixed issue with configuration options not being applied Signed-off-by: Tihomir Mateev <tihomir.mateev@redis.com>
1 parent e342062 commit 87dd0f2

2 files changed

Lines changed: 78 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public JedisClientConnection(@NonNull UnifiedJedis client, @NonNull JedisClientC
113113
Assert.notNull(clientConfig, "JedisClientConfig must not be null");
114114

115115
this.client = client;
116+
117+
// Select the configured database to ensure clean state
118+
// This matches the behavior of the legacy JedisConnection which always selects the database in the constructor
119+
// to ensure connections from the pool start with the expected database, regardless of what previous operations did
120+
select(clientConfig.getDatabase());
116121
}
117122

118123
private static DefaultJedisClientConfig createConfig(int dbIndex, @Nullable String clientName) {

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

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
2627
import org.jspecify.annotations.Nullable;
2728
import org.springframework.beans.factory.DisposableBean;
2829
import org.springframework.beans.factory.InitializingBean;
@@ -504,7 +505,14 @@ public boolean isRunning() {
504505
* @return the {@link RedisClient} to use. Never {@literal null}.
505506
*/
506507
protected RedisClient createRedisClient() {
507-
return RedisClient.builder().hostAndPort(getHostName(), getPort()).clientConfig(this.clientConfig).build();
508+
var builder = RedisClient.builder().hostAndPort(getHostName(), getPort()).clientConfig(this.clientConfig);
509+
510+
// Configure connection pool if pool configuration is provided
511+
clientConfiguration.getPoolConfig().ifPresent(poolConfig -> {
512+
builder.poolConfig(createConnectionPoolConfig(poolConfig));
513+
});
514+
515+
return builder.build();
508516
}
509517

510518
/**
@@ -519,9 +527,17 @@ protected RedisSentinelClient createRedisSentinelClient() {
519527

520528
JedisClientConfig sentinelConfig = createSentinelClientConfig(config);
521529

522-
return RedisSentinelClient.builder().masterName(config.getMaster() != null ? config.getMaster().getName() : null)
530+
var builder = RedisSentinelClient.builder()
531+
.masterName(config.getMaster() != null ? config.getMaster().getName() : null)
523532
.sentinels(convertToJedisSentinelSet(config.getSentinels())).clientConfig(this.clientConfig)
524-
.sentinelClientConfig(sentinelConfig).build();
533+
.sentinelClientConfig(sentinelConfig);
534+
535+
// Configure connection pool if pool configuration is provided
536+
clientConfiguration.getPoolConfig().ifPresent(poolConfig -> {
537+
builder.poolConfig(createConnectionPoolConfig(poolConfig));
538+
});
539+
540+
return builder.build();
525541
}
526542

527543
/**
@@ -536,7 +552,59 @@ protected RedisClusterClient createRedisClusterClient() {
536552

537553
Set<HostAndPort> nodes = convertToJedisClusterSet(config.getClusterNodes());
538554

539-
return RedisClusterClient.builder().nodes(nodes).clientConfig(this.clientConfig).build();
555+
var builder = RedisClusterClient.builder().nodes(nodes).clientConfig(this.clientConfig);
556+
557+
// Configure connection pool if pool configuration is provided
558+
clientConfiguration.getPoolConfig().ifPresent(poolConfig -> {
559+
builder.poolConfig(createConnectionPoolConfig(poolConfig));
560+
});
561+
562+
return builder.build();
563+
}
564+
565+
/**
566+
* Creates a {@link ConnectionPoolConfig} from the provided {@link GenericObjectPoolConfig}. Maps all available Apache
567+
* Commons Pool2 configuration options to Jedis ConnectionPoolConfig.
568+
*
569+
* @param poolConfig the pool configuration from Spring Data Redis
570+
* @return the Jedis ConnectionPoolConfig with all options applied
571+
*/
572+
private ConnectionPoolConfig createConnectionPoolConfig(GenericObjectPoolConfig poolConfig) {
573+
ConnectionPoolConfig connectionPoolConfig = new ConnectionPoolConfig();
574+
575+
// Basic pool settings
576+
connectionPoolConfig.setMaxTotal(poolConfig.getMaxTotal());
577+
connectionPoolConfig.setMaxIdle(poolConfig.getMaxIdle());
578+
connectionPoolConfig.setMinIdle(poolConfig.getMinIdle());
579+
connectionPoolConfig.setBlockWhenExhausted(poolConfig.getBlockWhenExhausted());
580+
connectionPoolConfig.setMaxWait(poolConfig.getMaxWaitDuration());
581+
582+
// Test settings
583+
connectionPoolConfig.setTestOnBorrow(poolConfig.getTestOnBorrow());
584+
connectionPoolConfig.setTestOnCreate(poolConfig.getTestOnCreate());
585+
connectionPoolConfig.setTestOnReturn(poolConfig.getTestOnReturn());
586+
connectionPoolConfig.setTestWhileIdle(poolConfig.getTestWhileIdle());
587+
588+
// Eviction settings
589+
connectionPoolConfig.setTimeBetweenEvictionRuns(poolConfig.getDurationBetweenEvictionRuns());
590+
connectionPoolConfig.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun());
591+
connectionPoolConfig.setMinEvictableIdleTime(poolConfig.getMinEvictableIdleDuration());
592+
connectionPoolConfig.setSoftMinEvictableIdleTime(poolConfig.getSoftMinEvictableIdleDuration());
593+
594+
// Ordering and fairness
595+
connectionPoolConfig.setLifo(poolConfig.getLifo());
596+
connectionPoolConfig.setFairness(poolConfig.getFairness());
597+
598+
// JMX and monitoring
599+
connectionPoolConfig.setJmxEnabled(poolConfig.getJmxEnabled());
600+
connectionPoolConfig.setJmxNamePrefix(poolConfig.getJmxNamePrefix());
601+
connectionPoolConfig.setJmxNameBase(poolConfig.getJmxNameBase());
602+
603+
// Advanced settings
604+
connectionPoolConfig.setEvictionPolicyClassName(poolConfig.getEvictionPolicyClassName());
605+
connectionPoolConfig.setEvictorShutdownTimeout(poolConfig.getEvictorShutdownTimeoutDuration());
606+
607+
return connectionPoolConfig;
540608
}
541609

542610
@Override
@@ -585,7 +653,7 @@ public RedisConnection getConnection() {
585653
}
586654

587655
JedisClientConfig config = this.clientConfig;
588-
redis.clients.jedis.UnifiedJedis client;
656+
UnifiedJedis client;
589657

590658
if (isRedisSentinelAware()) {
591659
SentinelConfiguration sentinelConfiguration = getSentinelConfiguration();

0 commit comments

Comments
 (0)