public void testConnectionPool()
           throws IOException, HttpException
   {
       final MultiThreadedHttpConnectionManager manager = new
MultiThreadedHttpConnectionManager();

       HttpClient httpClient = new HttpClient(manager);
       httpClient.getHostConfiguration().setHost("www.slashdot.org", 80,
"http");
       httpClient.setHttpConnectionFactoryTimeout(2000); // wait up to 2
seconds when getting a HttpConnection
       for (int i = 0; i < 30; i++) {
           HttpMethod method = new
GetMethod("http://www.slashdot.org/notfound";);
           int res = httpClient.executeMethod(method);
           // System.gc();
           // method.releaseConnection();
       }
   }

Uncommenting either of the last two lines makes the problem go away...

Yes, but you have done a very bad thing here. By doing the executeMethod(), you have established a connection to the server, and have read the headers. But then you are closing the connection before reading the body. The client (thats you!) is responsible for completing the read by calling one of the getResponseBody() methods. Even though you are getting 404 in this case, you don't know that and must still read the body after the execute.

The connection will be closed if there is a "Connection: Close" header automagicly only *after* you have read the body. Otherwise it will stay open awaiting the next request. releaseConnection() will forcibly close the connection regardless of the "Connection" header.

Another note about your example above, you don't need to bother setting the HostConfiguration if you are using fully qualified urls for the GetMethods.

I am surprised that calling System.gc() does anything here though. There is still a handle on the method at this point, and the connection is still in use so neither are elligible for garbage collection ...

Jandalf.




----- Original Message -----
From: "Michael Becke" <[EMAIL PROTECTED]>
To: "Commons HttpClient Project" <[EMAIL PROTECTED]>
Sent: Sunday, February 02, 2003 6:18 AM
Subject: Re: Running out of connections



Hello Simon,

Sorry to be replying so late. Connections are released when:

1) the response is fully read
2) the connection is manually released via
HttpMethod.releaseConnection() or HttpConnection.releaseConnection()
3) the garbage collector runs and reclaims any connections that are no
longer being used

The most reliable way is to manually release the connection after use.
This goes for successful or unsuccessful requests. Can you send a
sample of the code you are using that causes this problem?

Mike

On Wednesday, January 29, 2003, at 09:04 PM, Simon Roberts wrote:


Gidday,

With the current CVS version, I seem to be having a problem where I
run out of connections to a server. It happens if I do a bunch of
HTTP operations that fail (404, as it happens) and the reply include a
"Connection: close". If no garbage-collect happens then the
connections are not freed!

Shouldn't we expire them if we're running out of connections?

Cheers, Simon

---------------------------------------------------------------------
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]




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

Reply via email to