Jedis,无法获得jedis连接:无法从池中获取资源

Bik*_*wal 9 spring redis jedis spring-data-redis

我已经看到几个线程中的答案,但没有为我工作,因为偶尔会出现问题,如果有任何人有任何想法,请问这个问题.

我使用的是jedis版本2.8.0,Spring Data redis版本1.7.5.和我们的缓存应用程序的redis服务器版本2.8.4.

我有多个缓存,用redis保存,get请求是从redis完成的.我使用spring数据redis API来保存和获取数据.

所有保存和获取工作正常,但偶尔低于例外:

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool | org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolorg.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:198)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:345)
org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:129)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:92)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:79)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:166)
org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:88)
org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:49)
Run Code Online (Sandbox Code Playgroud)

我的redis配置类:

@Configuration
public class RedisConfiguration {

@Value("${redisCentralCachingURL}")
private String redisHost;

@Value("${redisCentralCachingPort}")
private int redisPort;

@Bean
public StringRedisSerializer stringRedisSerializer() {
  StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  return stringRedisSerializer;
}

@Bean
JedisConnectionFactory jedisConnectionFactory() {
  JedisConnectionFactory factory = new JedisConnectionFactory();
  factory.setHostName(redisHost);
  factory.setPort(redisPort);
  factory.setUsePool(true);
  return factory;
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
  RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  redisTemplate.setConnectionFactory(jedisConnectionFactory());
  redisTemplate.setExposeConnection(true);
  // No serializer required all serialization done during impl
  redisTemplate.setKeySerializer(stringRedisSerializer());
  //`redisTemplate.setHashKeySerializer(stringRedisSerializer());
  redisTemplate.setHashValueSerializer(new GenericSnappyRedisSerializer());
  redisTemplate.afterPropertiesSet();
  return redisTemplate;
}

@Bean
public RedisCacheManager cacheManager() {
  RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
  redisCacheManager.setTransactionAware(true);
  redisCacheManager.setLoadRemoteCachesOnStartup(true);
  redisCacheManager.setUsePrefix(true);
  return redisCacheManager;
 }

 }
Run Code Online (Sandbox Code Playgroud)

有没有人遇到这个问题或对此有任何想法,为什么会发生这种情况?

woe*_*ann 7

我们在使用 RxJava 时遇到了同样的问题,应用程序运行良好,但一段时间后,无法再从池中获取连接。经过几天的调试,我们终于弄清楚了问题的原因:

redisTemplate.setEnableTransactionSupport(true)
Run Code Online (Sandbox Code Playgroud)

不知何故导致 spring-data-redis 不释放连接。我们需要对 MULTI / EXEC 的事务支持,但最终改变了实现来解决这个问题。

我们仍然不知道这是否是一个错误或我们这边的错误用法。


Bik*_*wal 6

我从 redis.template 迁移到普通的 jedis。为池添加了以下配置(也可以在 redis 模板中添加),现在没有看到任何异常:

jedisPoolConfig.setMaxIdle(30);
jedisPoolConfig.setMinIdle(10);
Run Code Online (Sandbox Code Playgroud)

对于redis模板:

jedisConnectionFactory.getPoolConfig().setMaxIdle(30);
jedisConnectionFactory.getPoolConfig().setMinIdle(10);
Run Code Online (Sandbox Code Playgroud)

上面相同的配置也可以添加到redis模板中。

  • 嗯,看来它也对我有用,但我不确定为什么。您介意详细说明一下吗?基本上我的问题是如果我的应用程序耗尽空闲连接怎么办。(当前连接数 &gt; 最大空闲数) (2认同)