在每次调用后释放 org.apache.commons.httpclient 的连接是一个好习惯吗?

问题描述 投票:0回答:1

我正在创建一个 HttpClient as

new HttpClient(new MultiThreadedHttpConnectionManager());

每次调用后释放 org.apache.commons.httpclient 的连接是一个好习惯吗?比如下面

final GetMethod getMethod = new GetMethod(uri);

try {
  mHttpClient.executeMethod(getMethod)
} catch (Exception e) {
 //
} finally {
  getMethod.releaseConnection();
}

来自官方文档https://hc.apache.org/httpclient-legacy/performance.html它说

HttpClient always does its best to reuse connections. Connection persistence is enabled by default and requires no configuration. Under some situations this can lead to leaked connections and therefore lost resources. The easiest way to disable connection persistence is to provide or extend a connection manager that force-closes connections upon release in the releaseConnection method.

没有具体说推荐什么。每次 executeMethod 后 releaseConnection 会降低性能还是有其他缺点?或者最好总是在每次执行方法后释放连接?

java apache httpclient apache-commons-httpclient
1个回答
0
投票

视具体场景而定。 TCP三次握手比较耗时,所以应该尽量使用同一个TCP连接,也称为长连接或长连接。在某些场景下,如果发送完一个请求后不需要再基于这个socket再发送请求,可以立即关闭连接。

© www.soinside.com 2019 - 2024. All rights reserved.