On Thu, 31 Dec 2020 06:03:26 GMT, Xue-Lei Andrew Fan <xue...@openjdk.org> wrote:

>> From looking at the javadoc for URLConnection, I think the close has to 
>> happen on the Input/OutputStreams.  I have the OutputStream inside a 
>> try-with-resources block.  But I think I need to do the same with the 
>> InputStream so both are closed at the end of the transaction.
>
> I general consider to use HttpURLConnection.disconnect() and I/O close.  Per 
> the  HttpURLConnection class specification, "Each HttpURLConnection instance 
> is used to make a single request but the underlying network connection to the 
> HTTP server may be transparently shared by other instances. Calling the 
> close() methods on the InputStream or OutputStream of an HttpURLConnection 
> after a request may free network resources associated with this instance but 
> has no effect on any shared persistent connection. Calling the disconnect() 
> method may close the underlying socket if a persistent connection is 
> otherwise idle at that time.".

In this case I don't think there's a shared persistent connection going on.  
The URLConnection object's entire lifecycle is within this one method and for 
one connection only.  But I do see your point here.  Maybe a try-finally with 
the disconnect inside the finally might be cleaner instead of 
try-with-resources around each stream.  I'll give that a spin and see what 
happens.

-------------

PR: https://git.openjdk.java.net/jdk/pull/1760

Reply via email to