Reputation: 311
Java HTTP connection pooling for persistent connections is described at http://download.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html. However, there is no indication if connections are ever evicted from the cache if they are not reused, and if so, what is the timeout length.
The only reference to this I found were the following comments from http://www.java.net/forum/topic/performance/general-performance-discussion/reusing-socket-connections-httpurlconnection-0:
"the keep alive sockets are subject to a timeout. If the server sends back a timeout in the HTTP header, then that timeout value is used. Otherwise, the timeout is only 5 seconds, after which the socket will not be reused [...] Unfortuantely, you cannot set the timeout; it is hardwared in the bowels of the implementation."
If true, this would explain some odd behavior we are seeing, and I wrote a small test case that seems to confirm this. (Using Java 1.6.20 on Windows.)
Does anyone know of any documentation on this? Also, the comment is several years old. Is the timeout still hardcoded, or can it be set?
Upvotes: 4
Views: 4211
Reputation: 32293
It sounds pretty logical. The documentation for network properties (outside the ones you can set through the main API) is here. AFAICT the only thing you can set is whether or not to use keep-alive and how many connections to pool.
In Java 7 they're apparently only checked at startup. There's no note to that effect on the java 6 docs but I assume that must be a documentation oversight then. So in principle, to turn off keepalive you'd have to pass it on the command line as:
java -Dhttp.keepalive=false ...
Or to make the pool per host smaller:
java -Dhttp.maxConnections=1 ...
You could also try with System.setProperty()
but that supposedly wouldn't work.
It would be interesting to know what problems this causes, perhaps there is a solution to be found somewhere?
Upvotes: 1