欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

redis使用连接池报错解决

程序员文章站 2022-06-12 21:28:08
...
redis使用十几小时就一直报异常

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 
    at redis.clients.util.Pool.getResource(Pool.java:22) 
    at com.derbysoft.jredis.longkeytest.BorrowObject.run(BorrowObject.java:22) 
    at java.lang.Thread.run(Thread.java:662) 
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object 
    at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1134) 
    at redis.clients.util.Pool.getResource(Pool.java:20) 

原因是没有回收资源导致
正确的做法是
ShardedJedis jedis = shardedJedisPool.getResource();
try {
	jedis.set(key, value);
} finally {
	shardedJedisPool.returnResource(jedis);
}