Java DNS Caching breaks…or not?
Java DNS queries sometime turn out a bit tricky. The following feat was one example: consider you are using a round-robin dns system. Every query of the dns record returns all possible ip addresses, mixed up:
Applications usually use the first entry, resulting in a per-query association of host and ip address. If the actual DNS query is not cached, it works as a continuing distribution of querys over the listed ips.
Turning off the DNS caching in Java is easy: set the “networkaddress.cache.ttl” property to “0″ disables it. But here comes the problem: if you have mixed IPv4 and IPv6 hosts in your DNS, Java will prefer the v6 hosts, listing them on top on the v4 addresses. This results in a bizarre behaviour: the list of ips in the returned list are mixed up – but in two groups: the mixed IPv6 host group appears on top of the mixed IPv6 group!
The solution is rather simple: just add “java.net.preferIPv4Stack=true” to the properties environment and the round robin works as espected.







