我已经在几个线程中看到了答案,但对我来说并不起作用,而且因为我的问题偶尔会发生,所以问这个问题是否有人有任何想法。
我使用的是jedis版本2.8.0,Spring Data redis版本1.7.5。和redis服务器版本2.8.4用于我们的缓存应用程序。
我有多个缓存保存在redis中,get请求是从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;
}
}有没有人遇到过这个问题,或者对此有任何想法,为什么会发生这种情况?
发布于 2017-08-24 16:45:13
我从redis.template搬到了普通的jedis。池添加如下配置(也可以在redis模板中添加),现在看不到异常:
jedisPoolConfig.setMaxIdle(30);
jedisPoolConfig.setMinIdle(10);对于redis模板:
jedisConnectionFactory.getPoolConfig().setMaxIdle(30);
jedisConnectionFactory.getPoolConfig().setMinIdle(10);同样的配置也可以添加到redis模板中。
发布于 2017-08-12 21:25:13
我们在RxJava上也遇到了同样的问题,应用程序运行得很好,但过了一段时间后,池中就再也没有连接了。经过几天的调试,我们终于找出了问题的原因:
redisTemplate.setEnableTransactionSupport(true)不知何故导致了spring-data-redis不释放连接。我们需要对MULTI / EXEC的事务支持,但最终改变了实现来解决这个问题。
我们仍然不知道这是一个bug还是我们这边的错误使用。
https://stackoverflow.com/questions/43492474
复制相似问题