Java Niggles

DNS Caching

InetAddress Class Documentation.

By default, the result of positive host name resolutions are cached forever, because there is no general rule to decide when it is safe to remove cache entries.

Personally I think this behaviour is just wrong. However, to force a look up you need to run your program with the following property set.

java -Dnetworkaddress.cache.ttl=0 foo

Sometimes you'll find this does work, maybe because ncsd is caching DNS queries for you which you can check by turning it off. Sometimes it just seems to ignore you anyway, I found this while using JBoss, and you have to fix your JDK for all programs by editing

/usr/java/j2sdk1.4.2/jre/lib/security/java.security

and adding

networkaddress.cache.ttl=0

To watch the DNS queries as they happen on linux for debugging, tcpdump is your friend,

tcpdump udp port 53