JDK-4330985 : If URLConnection fails to getInputStream on first try, it never connects again
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2000-04-17
  • Updated: 2001-02-02
  • Resolved: 2001-02-02
Related Reports
Duplicate :  
Description

Name: stC104175			Date: 04/17/2000


java version "1.2.2"
Classic VM (build JDK-1.2.2-001, native threads, symcjit)

I'm having an odd problem trying to gracefully handle network problems with URL
connections (actually HTTP connections) within a java application (running
standalone -- not an applet and not running in a browser).

I'm looping on a call to an HTTP server. I want to get the HTML and continue to
update my java app with information from the retrieved HTML. I can open streams
to the HTTP server and get the response. Not a problem. I even handle cases
where calling getInputStream() on a URLConnection throws an IOException due to
network failure. In this case, I continue to loop until the network becomes
available. When it comes back up, the getInputStream() eventually works and
everything is fine.

Here's the problem... this only works if the **FIRST** time I call
getInputStream(), it succeeds! If the network is down the **FIRST** time I call
getInputStream(), every subsequent call to getInputStream() fails, regardless
of whether or not the network comes back up!

  To see what was going on, I wrote a couple of test classes. They try to connect
to the original server. If this fails (due to a network problem) they sleep
long enough for me to bring the network back up (plugging the cable back in)
and then they try to connect to a *Different* server. If the network is up by
then, the connection to the second server will succeed, while the connection to
the first server will continue to fail on every subsequent call!
  To get the communication to work, I have to quit my app and restart it. Then,
if the *First* try at connecting works, everything is fine. Plus, it doesn't
matter what server I try - this happens with every server I have tried.

The first time it fails, it hangs for about 20-30 seconds trying to connect
before throwing the exception. Every subsequent failure returns instantly with
an IOException error!  It looks as if it is caching the fact that the server
wasn't found and thus, never trys to connect to it again?!  I've tried
setUseCaches(false) with no change in behavior.

I have yet to find a workaround or a way to turn this caching off (if this is
really what is happening). I posted to the JDC and to Earthweb and have
received no help.  So, I searched for a workaround and looking at the source
for InetAddress, it uses an undocumented class sun.net.InetAddressCachePolicy.
I was unable to find documentation or source for InetAddressCachePolicy, so I
decompiled it. It has two settings FOREVER and NEVER.  However, the System
class is setting it to FOREVER in a way that I can't override.
InetAddressCachePolicy has a function setIfNotSet that is a public static
function.  However, using it throws a SecurityException with a message "can't
make InetAddress cache more lax", since it was already set to FOREVER
(determined by using InetAddressCachePolicy.get(), and comparing the returned
value to InetAddressCachePolicy.NEVER and InetAddressCachePolicy.FOREVER). I
don't even know if this is where the problem lies, or if there is a simple
workaround to my problem, but I've looked everywhere.

Below is the test code I'm using to display retrieved HTML from a URL:

-----------------------------
import java.io.*;
import java.net.*;

<<Rest of class ommitted>>

public void DisplayURL() throws Exception
{
  URL url = new URL("http://www.sun.com");
  URLConnection conn = url.openConnection();
  conn.setUseCaches(false);
  conn.connect();
  BufferedReader in = new BufferedReader(new InputStreamReader
(conn.getInputStream()));

  String inputLine = "";
 
  while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine + "\n");

  in.close();
}
-----------------------------

Also, this is a major problem for our application. If there is a known
workaround, or a solution that I have missed, PLEASE let me know.

Any help would be appreciated.

Thank you for your time.

Anthony Glaviano
###@###.###
(Review ID: 103668) 
======================================================================

Comments
EVALUATION There are a number of issues here :- 1. Caching of negative lookups is no longer the default in merlin so if the host lookup fails and later we try to resolve the host it may succeed on subsequent attempts if the network or name server is available. 2. Invoking getInputStream on the same instance of HttpURLConnection is not intended to retry to connection. Second and subsequent calls to getInputStream will always return the same input stream (or throw an exception if the first call to getInputStream returned by an exception). Based on the above I am closing this as a duplicate of 4256129. alan.bateman@ireland 2001-02-02
02-02-2001