Could not get a resource from the pool
配置
#redis的服务器地址
redis.host=165.****.78
#redis的服务端口
redis.port=6379
#密码
redis.pass=
#链接数据库
redis.default.db=0
#客户端超时时间单位是毫秒
redis.timeout=100000
#最大连接数
redis.maxActive=300
#最大空闲数
redis.maxIdle=100
#最大建立连接等待时间
redis.maxWait=100
redis.testOnBorrow=true
跟踪了一下源码,发现这句话出现在这里,在pool.java
文件:
//poo.java
public T getResource() {
try {
return internalPool.borrowObject();
} catch (NoSuchElementException nse) {
throw new JedisException("Could not get a resource from the pool", nse);
} catch (Exception e) {
throw new JedisConnectionException("Could not get a resource from the pool", e);
}
}
可以发现,是因为抛出了NoSuchElementException
这个异常,现在要找是哪里抛出了这个异常
internalPool.borrowObject()
方法:
public class GenericObjectPool<T> extends BaseGenericObjectPool<T>
implements ObjectPool<T>, GenericObjectPoolMXBean, UsageTracking<T> {
// ...
public T borrowObject() throws Exception {
return borrowObject(getMaxWaitMillis());
}
}
public T borrowObject(long borrowMaxWaitMillis) throws Exception {
//...
if (p == null) {
if (borrowMaxWaitMillis < 0) {
p = idleObjects.takeFirst();
} else {
p = idleObjects.pollFirst(borrowMaxWaitMillis,
TimeUnit.MILLISECONDS);
}
}
if (p == null) {
throw new NoSuchElementException(
"Timeout waiting for idle object");
}
//...
}
//...
好吧,最有可能的原因就是borrowMaxWaitMillis
值设置的太小,导致获取不到p
解决方案
尝试将config.properties
文件中redis的属性maxWait
调大一些
#最大建立连接等待时间
# redis.maxWait=100
redis.maxWait=20000
之后就连接成功了,nice!