views:

413

answers:

2

As can be seen in this document, a number of preconditions have to be met before TCP connection pooling "works" in java when using URLConnection. How can I determine if the connections are being pooled ? Are there any objects I can inspect in the sun jdk for this ?

+1  A: 

It's not really connection pooling, but the HTTP keep-alive mechanism that this document discusses. If you can inspect the network traffic in any way, then a simple way to detect this is to examine the request. If it's using HTTP/1.1 and there is no "Connection: close", or it's using HTTP/1.0 and you do see "Connection: Keep-Alive", then it's working as far as the request is concerned.

Otherwise it's pretty much an implementation detail. You could use a debugger to examine the internal state of the implementation and see if it seems to have some field set that enables keep-alive. You'd need to look at the source for this.

Also your doc suggests that you might look at System.getProperty("http.keepAlive"). If it's null or "true" then it seems that keep-alive is enabled.

Sean Owen
+1  A: 

For Sun's HTTP protocol handler included in Sun JDK 5, you can examine class sun.net.www.http.HttpClient. The member KeepAliveCache kac holds the persistent connections. If your connection is in the HashTable, it will be reused.

Depending on your usage pattern, the Keepalive support in HTTP handler can be problematic. When the connection stays in the cache, there is no thread responding to state change so the connection gets stale. You may get IOException when it's re-used. So we normally disable keepalive.

If you really need pooled connections, a better option is Apache HttpClient with pooled multi-thread connection manager.

ZZ Coder