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

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倍


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值