httpclient3.1的relaseConnection的misunderstand

releaseConnection是close connection,基于连接池的认识,我们首先会想到是释放链接,至于是否close就不得而知了。近日发现线上系统出现了close wait的socket,close wait是由于被动关闭的一方没有执行socket.close()导致,由此可见我们code中存在没有正确关闭socket。

类似的code是这样的

HttpClient httpClient = newHttpClient();

GetMethod getMethod = new GetMethod();


httpclient.execute(getMethod);
>>>>省略

finally

   getMethod.releaseConnection()
以上code存在一个方法体内,即每调用一次即创建一个httpclient,然后调用完毕销毁这个对象。
HttpClient默认使用SimpleHttpConnectionManager,这个connection manager维持一个connection,httpclient执行GetMethod的时候借用connection manager的connection,并标识in_use为true,而releaseConnection,只是归还并将in_use标记为false以供下次使用而不是真正的close。
    结论:
          1.releaseConnection不会close connection,适合的应用场景是构造一个httpclient,连续多个请求,而且并不是线程安全的。

          2. 解决方案:
             2.1 SimpleHttpConnectionManger的alwaysClose设置为true,这时候release就是真的close了
            2.2 since3.1 增加了shutdown方法调用结束shundown  ConnectionManager


 public void releaseConnection(HttpConnection conn) {
        if (conn != httpConnection) {
            throw new IllegalStateException("Unexpected release of an unknown connection.");
        }
        if (this.alwaysClose) {
            httpConnection.close();
        } else {
            // make sure the connection is reuseable
            finishLastResponse(httpConnection);
        }
        
        inUse = false;

        // track the time the connection was made idle
        idleStartTime = System.currentTimeMillis();
    }

猜你喜欢

转载自xiaoz5919.iteye.com/blog/1533785
3.1
今日推荐