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

Discussion in 'Java' started by noone, Nov 10, 2003.

  1. noone

    noone Guest

    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 ?
    noone, Nov 10, 2003
    #1
    1. Advertising

  2. noone

    Real Gagnon Guest

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

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


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

    Bye.
    --
    Real Gagnon from Quebec, Canada
    * Looking for Java or PB snippets ? Visit Real's How-to
    * http://www.rgagnon.com/howto.html
    Real Gagnon, Nov 10, 2003
    #2
    1. Advertising

  3. noone

    noone Guest

    Real Gagnon wrote:
    >>Subsequent socket connections by the JVM to that same FQDN uses the
    >>previous result instead of retrying the DNS query.

    >
    >
    > 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 ...
    noone, Nov 10, 2003
    #3
  4. noone

    noone Guest

    noone wrote:
    >
    > 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.
    noone, Nov 11, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Raskin
    Replies:
    0
    Views:
    389
    Dave Raskin
    Apr 26, 2004
  2. Linus
    Replies:
    1
    Views:
    5,651
    Tilman Bohn
    Feb 25, 2005
  3. Sideswipe

    JVM DNS cache flush

    Sideswipe, Nov 7, 2007, in forum: Java
    Replies:
    3
    Views:
    4,403
    Sideswipe
    Nov 30, 2007
  4. Mathias Lin
    Replies:
    1
    Views:
    754
    Mathias Lin
    Sep 15, 2010
  5. saggar
    Replies:
    2
    Views:
    1,321
    lewbloch
    Jun 17, 2011
Loading...

Share This Page