3

After my first post dealing with DNS,which is still not resolved: JVM and OS DNS Caching ,i am facing a new problem.

First here is my use case: i want to check if my private DNS is alive. If not i want to use a general DNS (e.g 8.8.8.8).

My private DNS (a bind9 on ubuntu with 192.168.1.188) as a specific record: test.testdnd.fr -> 192.168.1.100

So i thought i could do this:

if(InetAddress.getByAddress(my_dns_ip_in_byte).isReachable()){
    System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
    System.setProperty("sun.net.spi.nameservice.nameservers", 192.168.1.188);
}else{
    System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
    System.setProperty("sun.net.spi.nameservice.nameservers", "8.8.8.8");
}
 InetAddress.getHostByAddress(test.testdnd.fr) -> unknow host exception

But if i only Do:

System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
System.setProperty("sun.net.spi.nameservice.nameservers", 192.168.1.188);
InetAddress.getHostByAddress(test.testdnd.fr) -> 192.168.1.100

So as soon as i called an InetAddress method before setting system.property, it seems that the system.property has no effect. Maybe because the JVM load the system DNS values when a InetAddress method is called even if it dont use DNS resolution.

Is that a standard behavior in the JVM or am i loosing something?

Any help is welcome,

Community
  • 1
  • 1
zeraDev
  • 381
  • 1
  • 3
  • 16
  • possible duplicate of [Setting DNS property not having any effect](http://stackoverflow.com/questions/11957221/setting-dns-property-not-having-any-effect) – frostmatthew Jul 02 '13 at 16:09
  • Its not really a duplicate, i want to use a private DNS server. – zeraDev Jul 03 '13 at 07:45
  • Are you sure `InetAddress.getByAddress(my_dns_ip_in_byte).isReachable()` is returning true? (if you haven't checked already you should add some debugging output to the if block to see) – frostmatthew Jul 03 '13 at 13:26
  • Yup InetAddress.getByAddress(my_dns_ip_in_byte).isReachable() is returning true, and if i put a log in both brace-block,i see it doesn't go by the "else" block. – zeraDev Jul 04 '13 at 13:50

2 Answers2

7

The problem is that the nameservice providers are loaded only once in InetAddress and cached for further use. The loading happens in the static initializer of the class InetAddress. So once InetAddress is initialized, setting or changing the sun.net.spi.nameservice.provider properties will have no more effect.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
0

The JVM only uses the DNS provider you set on lookups that fail using the system's default. You can see this in the documentation (http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html), relevant excerpts:

By default, Java will use the system configured name lookup mechanism

You can specify a comma separated list of IP addresses that point to the DNS servers you want to use

In JDK 7, providers are chained, which means that if a lookup on a provider fails, the next provider in the list is consulted to resolve the name.

Community
  • 1
  • 1
frostmatthew
  • 3,260
  • 4
  • 40
  • 50
  • Yeah, but in my case i try to resolved a private record (not present in public DNS). So normally the JVM should try to use the next provider (my private DNS) but i doesn't if i called a network method before setting properties – zeraDev Jul 03 '13 at 07:44