spring Jedis,无法获得 jedis 连接:无法从池中获取资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43492474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jedis, Cannot get jedis connection: cannot get resource from pool
提问by Bikas Katwal
I have seen answers in couple of threads but didn't work out for me and since my problem occurs occasionally, asking this question if any one has any idea.
我已经在几个线程中看到了答案,但对我来说没有用,而且由于我的问题偶尔会发生,如果有人有任何想法,请询问这个问题。
I am using jedis version 2.8.0, Spring Data redis version 1.7.5. and redis server version 2.8.4 for our caching application.
我正在使用 jedis 2.8.0 版,Spring Data redis 1.7.5 版。和 redis 服务器版本 2.8.4 用于我们的缓存应用程序。
I have multiple cache that gets saved in redis and get request is done from redis. I am using spring data redis APIs to save and get data.
我有多个缓存保存在 redis 中,获取请求是从 redis 完成的。我正在使用 spring 数据 redis API 来保存和获取数据。
All save and get works fine, but getting below exception occasionally:
所有 save 和 get 工作正常,但偶尔会低于异常:
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)
My redis configuration class:
我的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;
}
}
Did anyone faced this issue or have any idea on this, why might this happen?
有没有人遇到过这个问题或对此有任何想法,为什么会发生这种情况?
采纳答案by Bikas Katwal
I moved from redis.template to plain jedis. Added below configuration(can be added in redis template too) for pool and don't see any exception now:
我从 redis.template 移到了普通的 jedis。为池添加了以下配置(也可以添加到 redis 模板中),现在看不到任何异常:
jedisPoolConfig.setMaxIdle(30);
jedisPoolConfig.setMinIdle(10);
for redis template:
对于 redis 模板:
jedisConnectionFactory.getPoolConfig().setMaxIdle(30);
jedisConnectionFactory.getPoolConfig().setMinIdle(10);
Same above config can be added in redis template too.
上面的配置也可以添加到 redis 模板中。
回答by woezelmann
We were facing the same problem with RxJava, the application was running fine but after some time, no connections could be aquired from the pool anymore. After days of debugging we finally figured out what caused the problem:
我们在使用 RxJava 时遇到了同样的问题,应用程序运行良好,但一段时间后,无法再从池中获取连接。经过几天的调试,我们终于找出了导致问题的原因:
redisTemplate.setEnableTransactionSupport(true)
somehow caused spring-data-redis to not release connections. We needed transaction support for MULTI / EXEC but in the end changed the implementation to get rid of this problem.
以某种方式导致 spring-data-redis 不释放连接。我们需要对 MULTI / EXEC 的事务支持,但最终改变了实现以摆脱这个问题。
Still we don't know if this is a bug or wrong usage on our side.
我们仍然不知道这是否是我们这边的错误或错误用法。

