java 我们是否应该在每次调用之后释放org.apache.commons.httpclient的Connection?

bwleehnv  于 2023-05-21  发布在  Java
关注(0)|答案(1)|浏览(156)

我正在创建一个HttpClient作为

new HttpClient(new MultiThreadedHttpConnectionManager());

我们是否应该在每次调用之后释放org.apache.commons.httpclient的Connection?比如下面

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之后释放连接是否会降低性能或有其他缺点?或者总是在每个executeMethod之后释放Connection更好?
我的剧本是不求长生的

dtcbnfnu

dtcbnfnu1#

这取决于具体的场景。TCP三次握手很耗时,所以你应该尝试使用相同的TCP连接,也称为持久连接或长连接。在某些情况下,如果您在发送一个请求后不需要基于此套接字发送任何进一步的请求,则可以立即关闭连接。

相关问题