Java HttpURLConnection and pooling

  1. Do both os and is need to be flushed and closed for the underlying socket to be reusable?

Closing the input stream is sufficient. You can’t flush an input stream, and flushing an output stream before close is redundant.

  1. Will connection.disconnect() close the underlying socket (and hence make it unreusable)?

It ‘acts as a hint’ to close the underlying connection.

Does keep-alive affect this behavior?

Yes. If it isn’t present, the connection must be closed.

  1. If I use different URL objects, but with the same URL, will the connections created from them share the underlying sockets?

Yes.

How about when the host part of the URL is the same, but paths are different?

Yes.

  1. When will pooled connections be destroyed?

After an idle timeout.

  1. What’s the system property that controls the size of the pool?

I’m not aware that there is one, but if there is it will be defined in the Networking Properties page which you can find via the Javadoc.

Additionally, if you could also compare the Android version with Java it would be great.

I believe that Android doesn’t do pooling at all, but this should change when they switch to the OpenJDK source.

Leave a Comment