I'm creating a HttpClient as
new HttpClient(new MultiThreadedHttpConnectionManager());
Should we releaseConnection for org.apache.commons.httpclient after each call? Such as below
final GetMethod getMethod = new GetMethod(uri);
try {
mHttpClient.executeMethod(getMethod)
} catch (Exception e) {
//
} finally {
getMethod.releaseConnection();
}
From the official doc https://hc.apache.org/httpclient-legacy/performance.html it says
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.
It's not specifically saying which will be better. Will releaseConnection after each executeMethod degrade performance or have another cons? Or it's better always to releaseConnection after each executeMethod?
My scenario is not long-living requests.
It depends on the specific scenario. The TCP three-way handshake is time-consuming, so you should try to use the same TCP connection, also known as a persistent connection or a long connection. In some scenarios, if you don't need to send any further requests based on this socket after sending one request, you can immediately close the connection.