You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Mohit Anchlia <mo...@gmail.com> on 2012/01/30 19:33:13 UTC

Releasing Connection

HttpClient 4.1.2 - I am using Multithreaded connection manager. Do I
need to explicitly call releaseConnection on this?

Also, after every http request I am calling
EntityUtils.toByteArray(entity), would it release connection or is
this the right way to release connection?

I am seeing some wierd behaviour and want to undersand this little more.

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Releasing Connection

Posted by Matt Narrell <ma...@gmail.com>.
Releasing the connection is managed by the aptly named ConnectionManager.  Consuming the entity (ultimately closing the InputStream) indicates that your usage of the current connection is complete.  The ConnectionManager will then decide what to do with the connection.  Read section 2.2 of [2] where connection persistence is discussed.  It's highly likely that your ConnectionManager is keeping this connection open for future requests.

If you must have the connection released, take a look at setting a ConnectionKeepAliveStrategy on the HttpClient.  Set the KeepAliveDuration to -1, and ensure you clean up your entities.

mn

On Jan 31, 2012, at 9:07 AM, Mohit Anchlia wrote:

> On Tue, Jan 31, 2012 at 8:24 AM, Matt Narrell <ma...@gmail.com> wrote:
>> You must consume the HttpEntity in every case, even if there is an exception, or you will leak connections from your ConnectionManager's pool.
>> 
>> You can consume entities via:
>> 
>> * Calling any of the EntityUtils.* methods.
> 
> But from looking at the code it looks like thatr
> "EntityUtils.toByteArray(entity), " also closes the stream. Does that
> release connection?
>> * Using a org.apache.http.impl.client.BasicResponseHandler (or any org.apache.http.client.ResponseHandler implementation that consumes the HttpEntity).
>> * Calling abort() on the HttpUriRequest in case of an exception.
>> See [1] for more information.
>> 
>> Do not call releaseConnection explicitly (unless you're implementing a ConnectionManager or working at low levels).  Doing so may conflict with any Keep-Alive strategies or connection persistence you might use.  See [2], section 2.8 for more information.
>> 
>> [1] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html
>> [2] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
>> 
>> On Jan 30, 2012, at 10:33 AM, Mohit Anchlia wrote:
>> 
>>> HttpClient 4.1.2 - I am using Multithreaded connection manager. Do I
>>> need to explicitly call releaseConnection on this?
>>> 
>>> Also, after every http request I am calling
>>> EntityUtils.toByteArray(entity), would it release connection or is
>>> this the right way to release connection?
>>> 
>>> I am seeing some wierd behaviour and want to undersand this little more.
>>> 
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>> 
>> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Releasing Connection

Posted by Mohit Anchlia <mo...@gmail.com>.
On Tue, Jan 31, 2012 at 8:24 AM, Matt Narrell <ma...@gmail.com> wrote:
> You must consume the HttpEntity in every case, even if there is an exception, or you will leak connections from your ConnectionManager's pool.
>
> You can consume entities via:
>
> * Calling any of the EntityUtils.* methods.

But from looking at the code it looks like thatr
"EntityUtils.toByteArray(entity), " also closes the stream. Does that
release connection?
> * Using a org.apache.http.impl.client.BasicResponseHandler (or any org.apache.http.client.ResponseHandler implementation that consumes the HttpEntity).
> * Calling abort() on the HttpUriRequest in case of an exception.
> See [1] for more information.
>
> Do not call releaseConnection explicitly (unless you're implementing a ConnectionManager or working at low levels).  Doing so may conflict with any Keep-Alive strategies or connection persistence you might use.  See [2], section 2.8 for more information.
>
> [1] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html
> [2] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
>
> On Jan 30, 2012, at 10:33 AM, Mohit Anchlia wrote:
>
>> HttpClient 4.1.2 - I am using Multithreaded connection manager. Do I
>> need to explicitly call releaseConnection on this?
>>
>> Also, after every http request I am calling
>> EntityUtils.toByteArray(entity), would it release connection or is
>> this the right way to release connection?
>>
>> I am seeing some wierd behaviour and want to undersand this little more.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Releasing Connection

Posted by Matt Narrell <ma...@gmail.com>.
You must consume the HttpEntity in every case, even if there is an exception, or you will leak connections from your ConnectionManager's pool.

You can consume entities via:

* Calling any of the EntityUtils.* methods.
* Using a org.apache.http.impl.client.BasicResponseHandler (or any org.apache.http.client.ResponseHandler implementation that consumes the HttpEntity).
* Calling abort() on the HttpUriRequest in case of an exception.
See [1] for more information.

Do not call releaseConnection explicitly (unless you're implementing a ConnectionManager or working at low levels).  Doing so may conflict with any Keep-Alive strategies or connection persistence you might use.  See [2], section 2.8 for more information.

[1] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html
[2] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

On Jan 30, 2012, at 10:33 AM, Mohit Anchlia wrote:

> HttpClient 4.1.2 - I am using Multithreaded connection manager. Do I
> need to explicitly call releaseConnection on this?
> 
> Also, after every http request I am calling
> EntityUtils.toByteArray(entity), would it release connection or is
> this the right way to release connection?
> 
> I am seeing some wierd behaviour and want to undersand this little more.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>