On Thu, 2007-09-06 at 08:40 +0200, Wittmann Armin wrote:
> Hi Oleg
> 
> > HttpClient keeps connections alive (open) infinitely unless told
> > otherwise, as it has no way of knowing when those connections may be
> > needed again. It is a responsibility of the application to make sure
> > open connections get closed if they are no longer needed. The
> > recommended way of doing that is by calling #closeIdleConnections()
> > method on the connection manager or by shutting it down.
> 
> Thanks für your answer.
> I will try to make 'my conclusions'.
> Since I am using the SimpleHttpConnectionManager that is not thread safe
> (in a multi thread environment as a servlet container as tomcat is) I 
> correctly do use the instance of the simple http-connection manager only as a 
> local
> variable that will be garbage collected after my http request has been made.
> 

One should _always_ shut down the connection manager if it is no longer
needed / used.  

Actually by creating a new connection manager on a per request you
effectively disable connection persistence, which can significantly
degrade performance. Consider reusing the connection manager between
requests if for some reason you are not willing to use the
multi-threaded (pooling) version.  

> But, to release ALL used internal resources it is not sufficient to allocate 
> the instance
> of the simple http-connection manager just locally, I also do need to use
> #closeIdleConnection (after a #releaseConnection) to free these network
> resources as well. (I checked this also looking into the code of the
> simple http-connection manager.)
> Correct?
> 

If you simply dereference an instance of a connection manager without
shutting it down, you leave it up to the garbage collector to close open
connections and reclaim allocated sockets, which can really take a
while. This can especially be a problem if you allocate connection
managers in a tight loop / on a per request basis.

Hope this helps

Oleg 


> Regards
> 
> Armin
> 
> ------------------------------------
> > > > My Code (simplified):
> > > > this code ist executed for every single request
> > > > 
> > > > HttpConnectionManager connectionManager = new 
> > > > SimpleHttpConnectionManager();
> > > > HttpClient client = new HttpClient(clientParams, connectionManager);
> > > > client.setHostConfiguration(hostConfiguration);
> > > > HttpMethod method =  = new GetMethod();
> > > > method.setQueryString(pairs);
> > > > method.setPath(pUrl.getPath());
> > > > method.setParams(methodParams);
> > > > try {
> > > >         client.executeMethod(method);
> > > > } catch (Exception e) {
> > > >         failed = true;
> > > >         throw new Exception(...);
> > > > } finally {
> > > >         if (failed) { 
> > > >                 method.abort();
> > > >                 method.releaseConnection();
> > > >         }
> > > > 
> > > >         client.setHttpConnectionManager(null);
> > > >         client = null;
> > > > }
> > > > 
> > > > try {
> > > >         responseString = method.getResponseBodyAsString();
> > > > } catch (Exception e) {
> > > >         throw new Exception(...);
> > > > } finally {
> > > >         method.releaseConnection();
> > > >         method = null;
> > > > }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to