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:

Mệnh đề if-else trong java
Java Program to Find the Mode in a Data Set
Immutable ArrayList in Java
Guide to Java OutputStream
How to Set TLS Version in Apache HttpClient
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement Solovay Strassen Primality Test Algorithm
Add Multiple Items to an Java ArrayList
Spring Boot - Cloud Configuration Server
Introduction to Java Serialization
Java Program to Implement Best-First Search
DistinctBy in the Java Stream API
Unsatisfied Dependency in Spring
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Supplier trong Java 8
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java – Write to File
wait() and notify() Methods in Java
Java Program to Implement Hash Tables with Quadratic Probing
Spring Boot - Zuul Proxy Server and Routing
Java Program to Represent Graph Using Adjacency Matrix
Java Program to Perform the Unique Factorization of a Given Number
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java Program to Implement Hash Tables Chaining with Binary Trees
How to Break from Java Stream forEach
Retrieve User Information in Spring Security
Collect a Java Stream to an Immutable Collection
Sorting in Java
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Guide to the Java Clock Class