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); }
时间: 2024-11-10 11:29:55