ConnectionPoolTimeoutException:Timeout waiting for connection from pool

【线上问题】Timeout waiting for connection from pool 问题排查 阅读详情
我做的是一个通过微信登录的商城,报错的地方就是登录的时候,起初在测试服务器上出现过一次ConnectionPoolTimeoutException:Timeout waiting for connection from pool

异常,不过当时因为忙着别的事情,而且重启服务器后就好了。再也没重现,之后发到生产服务器就开始大量的报此异常,登录这里全部阻塞住了。首先解决问题让用户可用,还是立马重启然后又好了,开始查询原因,异常如下:

2017-03-02 15:36:55  INFO WeChatServiceImpl:389 - 微信获取用户token,接口oauth2getAccessToken,start
2017-03-02 15:37:00  INFO WeChatServiceImpl:397 - 微信获取用户token
org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
	at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:286)
	at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:263)
	at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:190)
	at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
	at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
	at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
	at com.yunejian.yejTb.webUtil.http.WechatUserTokenRequestExecutor.execute(WechatUserTokenRequestExecutor.java:41)
	at com.yunejian.yejTb.webUtil.http.WechatUserTokenRequestExecutor.execute(WechatUserTokenRequestExecutor.java:22)
	at com.yunejian.yejTb.service.impl.WeChatServiceImpl.oauth2getAccessToken(WeChatServiceImpl.java:390)
	at com.yunejian.yejTb.service.impl.WeChatUserServiceImpl.getOpenId(WeChatUserServiceImpl.java:67)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.cache.interceptor.CacheInterceptor$1.invoke(CacheInterceptor.java:52)
	at org.springframework.cache.interceptor.CacheAspectSupport.invokeOperation(CacheAspectSupport.java:345)
	at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:414)
	at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
	at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
	at com.sun.proxy.$Proxy161.getOpenId(Unknown Source)
	at com.yunejian.yejTb.controller.api.WeChatController.login(WeChatController.java:61)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)


首先分析异常,是在CloseableHttpClient执行请求时候抛出的,从连接池中获取链接的时间太长了,超时报错在PoolingHttpClientConnectionManager中,

    protected HttpClientConnection leaseConnection(Future<CPoolEntry> future, long timeout, TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
        try {
            CPoolEntry entry = (CPoolEntry)future.get(timeout, tunit);//此处报错
            if(entry != null && !future.isCancelled()) {
                Asserts.check(entry.getConnection() != null, "Pool entry with no connection");
                if(this.log.isDebugEnabled()) {
                    this.log.debug("Connection leased: " + this.format(entry) + this.formatStats((HttpRoute)entry.getRoute()));
                }

                return CPoolProxy.newProxy(entry);
            } else {
                throw new InterruptedException();
            }
        } catch (TimeoutException var7) {
            throw new ConnectionPoolTimeoutException("Timeout waiting for connection from pool");
        }
    }



分析代码问题出在用Future.get()来获取链接实体,当第一个请求超时的时候,剩下的调用Future.get()就全部获取连接超时,造成这种情况的原因是,在获取CloseableHttpClient对象的时候使用了单例模式。

    protected CloseableHttpClient getHttpclient() {
        if(null != httpClient){  //只有一个client引起的

            return httpClient;
        }
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(10000)
                .setConnectTimeout(10000)
                .setConnectionRequestTimeout(5000)
                .build();
        httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
        return httpClient;

    }


但是还是有疑问,单例是产生了阻塞的问题,但是为什么报错的地方是Futrue.get呢,因为get方法中的timeOut是去的ConnectionRequestTimeOut,当时设置时候并没有在意这个参数,设置了5s,但是连接超时时已经有10s了。所以再到这就必然报错,为了验证 我把ConnectionRequestTimeOut设置为比ConnectionTimeout的时间短一些,果然报错就是connect timed out了,至此情况已经明了下一步解决问题。
最快也是最简单的解决办法就是取消单例模式,每次都创建一个对象,但是这么做会不会导致闲置的client太多呢,而且创建的成本本身也是很高的。所以我做了一次压测(之前就是因为没有做并发测试,不然早就发现这个问题了),模拟了100的并发量,发现没问题,通过JProfile发现堆中几乎没什么变化,因为在创建httpClient的具体显示中使用了一个connManager来管理连接,是从连接池中取的,所以没有问题。
总结下:首先项目没有进行压力测试这是问题没有及时发现并且重视的主要原因,其次在试用httpclient相关类尤其是参数设置的时候没仔细研究,都是想当然的使用,不可取,不可取。

线上Timeout waiting for connection from pool问题分析和解决方案 可以看到第1条,是老生常谈的资源关闭问题。博主在开发的时候,正是因为不需要知道调用的返回结果,所以没有对Response进行处理,所以资源并没有释放,导致后面再使用HttpClient,就拿不到连接了。可以看到确实默认值是maxConnPerRoute=2、maxConnTotal=20,并且我代码外部没有修传值。第2条,需要知道maxConnTotal和maxConnPerRoute的具体含义,如果是需要。,那么即使maxConnTotal设置再大,还是受限制与maxConnPerRoute的大小。 阅读详情

相关推荐

【已解决】Error:Connection timed out: connect

问题描述: 下载一个android源码,导入android studio后提示此错误: Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle 原因分析: 其实就是因为网络原因无法下载到项目所需要的以来的资源,包括各类jar包、gradle等 解决方案: 两个解决方案: 使用国内源代替jcenter和

游逸的博客 4万+

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool异常处理

出现这个问题是由于并发量大,不断向服务器发起请求。优化HTTP调用接口代码。

codesweetpotato的博客 2321

HttpClient连接池出现连续ConnectionPoolTimeoutException:Timeout waiting for connection from pool异常

在写一个测试Demo的时候,需要重复请求api,来测试返回结果的正确性和服务器搜索的压力。 但是Demo执行几十次之后会无限的出现ConnectionPoolTimeoutException这个异常。 测试代码如下: 主程序 public static void main(String[] args){ String[] mountains = KeyWords.mo

火山的博客 4223

HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查

1 错误信息 Https请求工具类] 发送 POST 请求(HTTP)错误, httpPost: org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool at org.apache.http.impl.conn.PoolingHttpClientConnecti...

u010325193的博客 1万+

HttpClient报ConnectionPoolTimeoutException: Timeout waiting for connection from pool

【现象】 org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:286) at org.apache.http.impl.conn

一火的专栏 1478

ConnectionPoolTimeoutException: Timeout waiting for connection from pool的错误排查及解决

ConnectionPoolTimeoutException: Timeout waiting for connection from pool的错误排查及解决 昨天遇到一个特别奇怪的事情,在系统中本来有个外部接口的方法,运行了好几年,一直正常,突然昨天开始出现异常,并且异常也比较奇怪,排查了好长时间。先看下原始代码: static { RequestConfig requestConfig = RequestConfig.custom() .setC

泗水长流的博客 3586

Apache HttpClient的ConnectionPoolTimeoutException: Timeout waiting for connection from pool异常

Apache HttpClient的ConnectionPoolTimeoutException: Timeout waiting for connection from pool异常

wutian90的博客 684

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool

qq_31817993的博客 4925

解决Httpclient 4 偶尔报错ConnectionPoolTimeoutException: Timeout waiting for connection from pool

HttpParams paramsw = new BasicHttpParams(); HttpConnectionParams.setStaleCheckingEnabled(paramsw, false); HttpConnectionParams.setConnectionTimeout(paramsw, CONNECTION_TIMEOUT); HttpConnectionParams.s

死脑袋瓜的专栏 1万+

记录一次restteamplat调用出现的ConnectionPoolTimeoutException: Timeout waiting for connection from pool

前言 服务突然异常,而且监控的dashboard某一个节点进场自动启停就像心电图一样。。。 排查 首先看了tomcat的占用,好家伙出问题的机器400多的占用连接,看了下服务的监控表,某一个需要调用三方ai的服务跑了了400、500秒!?怎么会这样,调用AI的restteamplate超时配置也就5秒。第一反应是restteamplat 配置失效了,查看日志,发现日志报了很多 Timeout waiting for connection from pool; nested exception is org.

aaaaa2428572的博客 1814

Timeout waiting for connection from pool 问题排查

Timeout waiting for connection from pool 实际场景问题排查及方案解决

知道的越多 不知道的就越多 3641

ConnectionPoolTimeoutException: Timeout waiting for connection

HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查 分类: Java基础解惑集 服务器系统架构2011-07-18 20:11 4260人阅读 评论(7) 收藏 举报 今天解决了一个HttpClient的异常,汗啊,一个HttpClient使用稍有不慎都会是毁

tanqiantot的专栏 1641

HttpClient:Timeout waiting for connection from pool

项目中访问外网http接口的逻辑,然后就直接用Apache的HttpClient包做了一个工具类进行调用,但项目上线后发现有大bug,报错:response stream exception,org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool,错误是Apache包...

小智的装逼时间 5935

HttpClient Timeout waiting for connection from pool 问题解决方案

错误:org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool 前言 :第一次看到这个错误, 上网找了下,有文章说是连接池不够了。。。。 并没有多想,立即将原有程序的 链接池扩容了3倍,然后单个路由 扩容了5倍。问题解决, 以为找到了,答案。 但是 过了大约 几...

weixin_33958585的博客 1079

【httpClient】Timeout waiting for connection from pool

1.场景1 1.1 概述 在做问题:【Flink】HttpClient 报错 I/O SocketException caught when processing request to Connection Reset 的时候,写入了如下测试类,然后运行报错 private CloseableHttpClient closeableHttpClient; @Before public void init(){ PoolingHttpClientConnectio

九师兄 762

HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection

HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查 http://blog.csdn.net/shootyou/article/details/6615051   使用httpclient必须知道的参数设置及代码写法、存在的风险

hualizide的专栏 4071

ClientHttpRequestInterceptor报错Timeout waiting for connection from pool

restTemplate实现ClientHttpRequestInterceptor,报错org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool。主要是针对restTemplate调用的接口进行重试处理,重试3次。

黄黄黄黄黄的博客 909
上一篇: 子类继承和调用父类的构造方法
下一篇: 从cobar到mycat的基本使用
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值