Question

I'm using an Apache DefaultHttpClient with a PoolingClientConnectionManager and BasicResponseHandler. These are shared between different threads, and each thread creates its own HttpRequestBase extension.

Do I need to manually tell the manager that I'm done using the connection when using BasicResponseHandlers? Do I need to wrap it in a finally so exceptions don't cause a connection leak?

In other words, do I need to do this

HttpGet get = new HttpGet(address);
try {
  httpclient.execute(get, new BasicResponseHandler());
} finally {
  get.reset();
}

or is this enough ?

HttpGet get = new HttpGet(address);
httpclient.execute(get, new BasicResponseHandler());

I didn't see a clear answer in the Apache documentation.

Was it helpful?

Solution

This is enough and is recommended.

HttpClient#execute methods are guaranteed to automatically release all resources associated with the request in case of an exception (either I/O or runtime). When an HTTP response is processed using a ResponseHandler the underlying connection gets automatically released back to the connection manager is all cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top