views:

248

answers:

2

Hi,

I use commons-httpclient to send queries to a Web server. The Web server never closes connections, but has a small memory leak associated to the connection context.

I would therefore like to close the persistent connections from time to time (every X queries for example), but I did not find any way to do this. I use the MultiThreadedHTTPConnectionManager

I could use MultiThreadedHTTPConnectionManager.closeIdleConnections(very_small_delay), but that's not very reliable: if my Java code has much work to do, then it's possible that no connection is ever idle, because there are always other threads waiting for a free connection.

Thanks,

A: 

Are you calling method.releaseConnection()?

http://hc.apache.org/httpclient-3.x/threading.html#Connection_Release

matt b
Yes, which has the correct effect of putting the connection back in the pool where it can be reused for another query.The additional thing I want is to explicitely kill the connection and forbid its reuse, from time to time (doing the client-side equivalent of settings Apache's max keep-alive requests)
Zorglub
+1  A: 

after executing one method, let's say GET, you can read the responsed HttpStatus number, to decide if a method.releaseConnection() needs to be called.

Or if you know which connection you wanna close, you could try

MultiThreadedHTTPConnectionManager
void    releaseConnection(HttpConnection conn) 

okay, I thought you have some reason that you want to 'kill' a connection. Assume you have the connection, the HttpConnection Object, which you want to close in hand. May this method help?

HttpConnection
public void close()

    Closes the socket and streams. 
Kent
Hi,calling releaseConnection() puts the connection back in the pool, which is not what I want, as it will stay alive. However, if I don't call releaseConnection(), then the pool will think I still use it, and not create another one, basically leading to a deadlock.
Zorglub
Duh, it was too obvious, now I feel stupid. Thanks !
Zorglub