gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:56:27 27 4
gpt4 key购买 nike

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

我使用的是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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com