Could not get a resource from the pool 错误分析

来源:互联网 发布:flash cc mac 编辑:程序博客网 时间:2024/06/05 11:41

1 Could not get a resource from the pool

1.1 当jedispool中的jedis被取完 等待超过你设置的 MaxWaitMillis 就会抛出Could not get a resource from the pool 

GenericObjectPool 源代码borrowObject(long borrowMaxWaitMillis)方法可以看出

 

if(p == null) {

    if(borrowMaxWaitMillis < 0L) {

p = (PooledObject)this.idleObjects.takeFirst();

    } else {

waitTime = System.currentTimeMillis();

p = (PooledObject)this.idleObjects.pollFirst(borrowMaxWaitMillis, TimeUnit.MILLISECONDS);

waitTime = System.currentTimeMillis() - waitTime;  //等待时间超过 borrowMaxWaitMillis 的时候 p =null

    }

}


if(p == null) {

    throw new NoSuchElementException("Timeout waiting for idle object");  抛出异常 被pool这个类捕捉

}

pool 源码

try {
            return this.internalPool.borrowObject();
        } catch (Exception var2) {
            throw new JedisConnectionException("Could not get a resource from the pool", var2);
        }


所以只要把jedis配置MaxWaitMillis 设置的大一点 就可以降低 由于MaxWaitMillis 导致的  

Could not get a resource from the pool 

【我自己设置MaxWaitMillis  = 20000

1.2 加快从jedispool中获取get jedis 和return jedis的 速度

设置 testOnBorrow、testOnReturn 都改为false 
在这两个配置为true的情况下 get 、 return  jedis的时候 jedis 将ping 一下redis

GenericObjectPool 源代码borrowObject(long borrowMaxWaitMillis)方法可以看出

if(p != null && (this.getTestOnBorrow() || create && this.getTestOnCreate())) {
boolean validate = false;
Throwable validationThrowable1 = null;


try {
validate = this.factory.validateObject(p); 获取直接先验证是否可以用
} catch (Throwable var13) {
PoolUtils.checkRethrow(var13);
validationThrowable1 = var13;
}

JedisFactory 源代码validateObject(PooledObject<Jedis> pooledJedis)方法可以看出

BinaryJedis jedis = (BinaryJedis)pooledJedis.getObject();

try {
return jedis.isConnected() && jedis.ping().equals("PONG");
} catch (Exception var4) {
return false;
}


【基本以上这么修改 就能够解决Could not get a resource from the pool  但是还是有其他的一些情况 我就不一一举例 有问题的可以看一下 GenericObjectPool 源码 基本上就明白

操作jedis 的时候 设置 testOnBorrow、testOnReturn 都改为false   要比true  快上1.4倍


0 0
原创粉丝点击