首页 > 代码库 > redis运用连接池报错解决

redis运用连接池报错解决

redis使用连接池报错解决
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);}

 

redis运用连接池报错解决