Redis在3.2版本后加入了连接池功能,这样可以在应用中通过池化redis连接提高服务性能。但是在连接池中获取连接实例有时候会出现一些异常错误,这里大概举例两个错误对应的原因:
1、如果在maxWaitMillis时间内仍然无法获取到jedis对象就会抛出如下异常:
redis.clients.jedis.exception.JedisConnectionException:Could not get a resource from the pool
.............
caused by: java.util.NoSuchElementException:Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:449)
2、还有一种情况,就是设置了blockWhenExhausted=false,那么在调用者发现尺子中没有idle资源时,会立即抛出异常不进行等待,下面的异常就是blockWhenExhausted=false时的效果:
redis.clients.jedis.exception.JedisConnectionException:Could not get a resource from the pool
.............
caused by: java.util.NoSuchElementException:Pool exhausted
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:464)