Java, DNS, and caching

September 27, 2006

I have blogged about Java and caching of DNS once before.

It looks like the peeps over on Java Lobby have run into this common problem too.

There are a couple of ways around this problem that dont involve restarting all of your Java network based applications.

There is a property that can be set via java.security.Security, “networkaddress.cache.ttl”, that sets the poll interval (in seconds) that Java will use before refreshing a cached DNS name.

To set this value, you just need to include the following snippet somewhere in your code:

bq. java.security.Security.setProperty(“networkaddress.cache.ttl” , “60″)

There is a second property, “networkaddress.cache.negative.ttl” which is pertinent too. This property sets the poll interval used for unsucessful DNS lookups.

You can set both of these properties to zero, which will disable DNS caching all together.

In addition to these settings, there are two internal system properties that can also be used, but depending on your VM, these may or may not work.

“sun.net.inetaddr.ttl” & “sun.net.inetaddr.negative.ttl” correspond directly to the two previously mentioned properties, but are System properties rather than Security properties, so they have the advantage of being configurable via the command line, or centralised with other application properties.

There is a useful page over on the Sun Java site that discusses all of the available Networking Properties. It is well worth bookmarking.


Moving

August 11, 2006

I’m not moving… the site is. I’m just in the process of moving it to a new host. It’ll take 48 hours or so for the DNS changes to propogate.