Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HttpUrlConnection leaves connections in CLOSED_WAIT state

843790Apr 2 2010 — edited Apr 2 2010
Our java application fetches pages from various websites. One of our customers is finding that their machines have a very large number of TCP/IP connections in CLOSED_WAIT states that are remaining for long periods of time. We have not been able to reproduce this -- any thoughts?

I have attached our basic processing code (with non-relevant code removed). We are running on Microsoft Windows Server 2003 and using Java 6 and the java.net.HttpURLConnection class. For each connection, we close the input stream after reading it, but do not disconnect, so that Java can re-use the connection. This normally produces many connections with TIMED_WAIT states, but only an occasional short-lived CLOSED_WAIT state.

URLConnection urlConnection = url.openConnection();
    urlConnection.setConnectTimeout(timeout);
    urlConnection.setReadTimeout(timeout);

    if (isPost()){
      ((HttpURLConnection)urlConnection).setDoOutput(true);
    }
    urlConnection.connect();   
    urlConnection.getResponseCode();
    
    try{
      try{
        contentsStream = urlConnection.getInputStream();
      }
      catch(IOException e){
        if (urlConnection instanceof HttpURLConnection){
          contentsStream = ((HttpURLConnection)urlConnection).getErrorStream();
        }
        if (contentsStream == null){
          throw e;
        }
      }
  
      String contentEncodingHeader = getResponseHeader().getOne("Content-Encoding");
      if (contentEncodingHeader != null){
        if (contentEncodingHeader.equalsIgnoreCase("gzip")){
          contentsStream = new GZIPInputStream(contentsStream);
        }
        else if (contentEncodingHeader.equalsIgnoreCase("deflate")){
          contentsStream = new InflaterInputStream(contentsStream, new Inflater(true));
        }
        else{
          throw new UnsupportedContentEncodingException(getRequest(), contentEncodingHeader);
        }
      }
      
      while (bytesRead = contentsStream.read(current)) >= 0) {
      }
    }
    finally{
        if (contentsStream != null){
          contentsStream.close();
        }
        //Don't disconnect, as we want to let Java re-use the connection if possible.
        //disconnect(urlConnection);
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 30 2010
Added on Apr 2 2010
3 comments
2,148 views