Skip to main content

Warning: Java caches DNS to IP address lookups forever!

Found an interesting problem this morning after the IP address of a web server was changed in our DNS.

Web browsers could see the site at the new address, the command line of ping server could see the site just fine, but the long-running Java pinger application could not see the server at all. All we received were messages informing us that it could not contact the moved server.

After some digging on the internet I quickly found the reason why:
networkaddress.cache.ttl (default: -1)
Specified in to indicate the caching policy for successful name lookups from the name service.. The value is specified as as integer to indicate the number of seconds to cache the successful lookup.

A value of -1 indicates "cache forever".
Sourced from: J2SE 1.4.2 Networking Properties

This is also mentioned in another post on how to Disable DNS caching.

So you need to restart the JVM to pick up recent DNS changes... or you could change the networkaddress.cache.ttl System property. But this doesn't work in some cases due to bug 6247501 - although the bug is closed, read the comments at the bottom of the page - not sure if the bug was closed after these comments or before...

Technorati Tags: , , , ,