UnknownHostException --- Does JVM cache results from DNS queries?

N

noone

JDK 1.3.1_09
Solaris 8

For some time now, every now and then, we would get an
UnknownHostException trying to connect to a 3rd party using their FQDN.
We retry the connetion ( HTTP, SMPP, etc. ) every 30 seconds and would
keep on getting UnknowHostException.

Whenever this happens, I would try to do an nslookup, and it would
return the 3rd party's IP address. These are fixed IP addresses.
I would then restart the JVM, and the problem will go away.


The only way I could explain this is that:

During a specific time when we tried to connect to the 3rd party, the
DNS server timed out, was unreachable, or returned an error.

Subsequent socket connections by the JVM to that same FQDN uses the
previous result instead of retrying the DNS query.


Anyone had this problem before ?
 
N

noone

Real said:
Yes , there is a caching mechanism, see
http://www.rgagnon.com/javadetails/java-0445.html .

Bye.

Interesting .... could not find the same document in the JDK 1.3 docs.
Compare these two:

http://java.sun.com/j2se/1.3/docs/guide/net/
http://java.sun.com/j2se/1.4.2/docs/guide/net/

Also, if that applies to 1.3, then the default for unsuccessful name
lookups ( the one I described ) is 10 seconds, while the problem I am
seeing is that JDK 1.3 seems to be caching unsuccessful lookups forever.

Oh well ... will experiment ...
 
N

noone

noone said:
Interesting .... could not find the same document in the JDK 1.3 docs.
Compare these two:

http://java.sun.com/j2se/1.3/docs/guide/net/
http://java.sun.com/j2se/1.4.2/docs/guide/net/

Also, if that applies to 1.3, then the default for unsuccessful name
lookups ( the one I described ) is 10 seconds, while the problem I am
seeing is that JDK 1.3 seems to be caching unsuccessful lookups forever.

Oh well ... will experiment ...

OK. Just tried it on JDK 1.3.1_08 on Solaris8 SPARC.

import java.net.*;
import java.util.*;

public class Test2 {

public static void main( String args[] ) throws Exception {
System.out.println( "sun.net.inetaddr.ttl: " + System.getProperty(
"sun.net.inetaddr.ttl") );
System.out.println( "sun.net.inetaddr.negative.ttl: " +
System.getProperty( "sun.net.inetaddr.negative.ttl") );

System.out.println( "networkaddress.cache.ttl: " +
System.getProperty( "networkaddress.cache.ttl") );
System.out.println( "networkaddress.cache.negative.ttl: " +
System.getProperty( "networkaddress.cache.negative.ttl") );

System.out.println( "sun.net.InetAddressCachePolicy.get(): " +
sun.net.InetAddressCachePolicy.get() );
System.out.println( "sun.net.InetAddressCachePolicy.FOREVER: " +
sun.net.InetAddressCachePolicy.FOREVER );
System.out.println( "sun.net.InetAddressCachePolicy.NEVER: " +
sun.net.InetAddressCachePolicy.NEVER );

InetAddress addr;

addr = InetAddress.getByName( "www.google.com.au" );
System.out.println( addr.getHostAddress() );
Thread.sleep( 5000 );

addr = InetAddress.getByName( "www.google.com.au" );
System.out.println( addr.getHostAddress() );
Thread.sleep( 15000 );

addr = InetAddress.getByName( "www.google.com.au" );
System.out.println( addr.getHostAddress() );
}

}


Now when I run the above, I also run tcpdump, like this:
snoop -d hme1 -r port 53

bash-2.03$ /usr/j2sdk1_3_1_08/bin/java -Dsun.net.inetaddr.ttl=0 Test2
sun.net.inetaddr.ttl: 0
sun.net.inetaddr.negative.ttl: null
networkaddress.cache.ttl: null
networkaddress.cache.negative.ttl: null
sun.net.InetAddressCachePolicy.get(): 0
sun.net.InetAddressCachePolicy.FOREVER: -1
sun.net.InetAddressCachePolicy.NEVER: 0
216.239.53.99
216.239.53.99
216.239.53.99


What is strange is, the first InetAddress.getByName() resulted in a DNS
query, which tcpdump showed. The second and third ones did not result in
a DNS query, even though sun.net.inetaddr.ttl is 0 ( never cache ).

What is more weird is that, after the test program terminated, I then
reran it, and no DNS queries were ever made again.

It seems that the OS itself is caching DNS results as well ?
But when I run "nslookup www.google.com.au", that DNS query by nslookup
shows up on tcpdump.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top