Could not get a resource from the pool

本文分析了一种常见的Redis连接池异常“Couldnotgetaresourcefromthepool”,通过跟踪源码定位问题为连接等待时间过短,并给出了增大maxWait参数后的有效解决方案。

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值