Home Redis异常:Could not get a resource from the pool
Post
Cancel

Redis异常:Could not get a resource from the pool

报错信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)  
... 2 more  
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)
... 2 more

产生原因

客户端去Redis服务器拿连接(代码描述的是租用对象borrowObject)的时候,池中无可用连接,即池中所有连接被占用,且在等待时候设定的超时时间后还没拿到时,报出此异常。

解决办法

调整JedisPoolConfig中maxActive为适合自己系统的阀值。

1
2
3
4
5
6
<bean id="dataJedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxActive" value="300" />
    <property name="maxIdle" value="100" />
    <property name="maxWait" value="10000" />
    <property name="testOnBorrow" value="true" />
</bean>  
This post is licensed under CC BY 4.0 by the author.