Disable DNS caching

JDK1.4

Once an application has performed network access (i.e. urlconnection, parsing of xml document with external references, etc), the DNS settings get cached so any subsequent operation will use the old settings even if the real settings have changed. To reset everything, you have to restart the server since the the default setting JVM setting is to cache forever.

There are 4 properties that can be used to override the default behaviour.

networkaddress.cache.ttl (default: -1)

Specified in java.security 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”.

networkaddress.cache.negative.ttl (default: 10)

Specified in java.security to indicate the caching policy for un-successful name lookups from the name service. The value is specified as as integer to
indicate the number of seconds to cache the failure for un-successful lookups. A value of 0 indicates “never cache”. A value of -1 indicates “cache forever”.

sun.net.inetaddr.ttl

This is a sun private system property which corresponds to networkaddress.cache.ttl. It takes the same value and has the same meaning, but can be set as a command-line option. However, the preferred way is to use the security property mentioned above.

sun.net.inetaddr.negative.ttl

This is a sun private system property which corresponds to networkaddress.cache.negative.ttl. It takes the same value and has the same meaning, but can be set as a command-line option. However, the preferred way is to use the security property mentioned above.
So you can disable caching by adding -Dsun.net.inetaddr.ttl=0 on the command line starting the JVM. But you can’t set the value of networkaddress.cache.ttl on the command line. You can set the required value in the java.security file located in %JRE%\lib\security

networkaddress.cache.ttl=60
networkaddress.cache.negative.ttl=10

or set the value in your code with:

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

JDK1.6 or more:

The default value has changed for:

networkaddress.cache.ttl

Done! Happy Coding!

Related posts:

Java Program to Generate Random Numbers Using Probability Distribution Function
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Spring AMQP in Reactive Applications
Java Program to Implement the MD5 Algorithm
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Sort a HashMap in Java
Send an email using the SMTP protocol
Setting Up Swagger 2 with a Spring REST API
How to Remove the Last Character of a String?
Guide to the Synchronized Keyword in Java
Java Program to Implement Self Balancing Binary Search Tree
Cài đặt và sử dụng Swagger UI
Java Program to Implement Park-Miller Random Number Generation Algorithm
Java Program to Implement Ternary Heap
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
How to Read HTTP Headers in Spring REST Controllers
HttpClient with SSL
Java Program to Solve Knapsack Problem Using Dynamic Programming
So sánh ArrayList và Vector trong Java
Java Program to Implement WeakHashMap API
Java Program to Create a Random Linear Extension for a DAG
Hướng dẫn Java Design Pattern – Factory Method
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Converting a Stack Trace to a String in Java
Guava CharMatcher
Java 14 Record Keyword
Java Program to Describe the Representation of Graph using Adjacency Matrix
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?