spring - Jedis,无法获得 Jedis 连接 : cannot get resource from pool

标签 spring redis jedis spring-data-redis

我在几个线程中看到了答案,但没有解决我的问题,因为我的问题偶尔会出现,如果有人有任何想法,请问这个问题。

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

我有多个缓存保存在 redis 中,获取请求是从 redis 完成的。我正在使用 spring data 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)

我的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;
 }

 }

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

最佳答案

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

redisTemplate.setEnableTransactionSupport(true)

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

我们仍然不知道这是错误还是我们这边的错误用法。

关于spring - Jedis,无法获得 Jedis 连接 : cannot get resource from pool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492474/

相关文章:

java - 如何在 Spring Data REST 中公开 @EmbeddedId 转换器

java - Facebook 之类的通知或 ajax 调用?

java - Spring Security 与 Redis 的并发控制

c# - 如何使用 stackexchange.redis 从多个键中获取所有哈希元素

redis - WSL Redis 遇到 System has not been booted with systemd as init system (PID 1)。不能操作

redis - 如何引入 Redis 只是为了缓存 no CRUD

java - 使用spring boot从redis读取字符串

java - 如何访问正在运行的 Spring Boot jar 中的日志文件?

redis - 有没有办法从 redis 获取所有禁用的 redis 命令的列表?

spring - 检查Apache Velocity中的映射是否为空