1

We have an application which connects to the different applications using hostname. If I change the DNS server of the system on which the application is running, the application starts to fail with "java.net.UnknownHostException". But from the system I can send ping requests to different applications using FQDN. To take effect of DNS server change, we have to restart the application.

Is there any way in Java where we can refresh the DNS server details at runtime ?

Rahul Vedpathak
  • 1,346
  • 3
  • 16
  • 30
  • 1
    We need more info. What type of server is this? What are the services that start to fail (e.g. a DB connection pool)? Is it all types of services? – Jason Armstrong Dec 17 '18 at 11:44
  • Its a tomcat server running a web app. From this application we connect to a web app using the FQDN. – Rahul Vedpathak Dec 17 '18 at 12:00
  • Please allow us Google that for you... [java dns cache](https://www.google.com/search?q=java+dns+cache), which leads directly to [How to make Java honor the DNS Caching Timeout?](https://stackoverflow.com/q/1256556/608639) – jww Dec 17 '18 at 19:22

1 Answers1

3

You can turn off dns caching for Java with 3 solutions:

Solution 1:

Start your program with this flag:

-Dsun.net.inetaddr.ttl=0

Solution 2:

At top of your java code use setProperty

java.security.Security.setProperty("networkaddress.cache.ttl" , "0")

Solution 3:

In $JRE_HOME/lib/security/java.security set :

networkaddress.cache.ttl = 0
networkaddress.cache.negative.ttl = 0
Mohsen
  • 4,536
  • 2
  • 27
  • 49