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 Switch Statement
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Cơ chế Upcasting và Downcasting trong java
Java – Try with Resources
Tính trừu tượng (Abstraction) trong Java
Hướng dẫn Java Design Pattern – Composite
Lớp lồng nhau trong java (Java inner class)
Java Program to Decode a Message Encoded Using Playfair Cipher
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to Implement Shell Sort
Java Program to Compute the Area of a Triangle Using Determinants
Guide to Mustache with Spring Boot
Java Program to Implement Randomized Binary Search Tree
Hướng dẫn sử dụng Java Reflection
Java Program to Implement LinkedBlockingDeque API
Spring Boot - Bootstrapping
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Spring Data – CrudRepository save() Method
Hướng dẫn Java Design Pattern – Builder
Java – File to Reader
Từ khóa static và final trong java
Circular Dependencies in Spring
Java Program to Implement LinkedBlockingQueue API
Java Program to Implement ConcurrentSkipListMap API
Array to String Conversions
Spring Boot Application as a Service
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Java Program to Implement Hash Tables with Quadratic Probing
XML Serialization and Deserialization with Jackson
Chuyển đổi giữa các kiểu dữ liệu trong Java
Guide to Guava Multimap
Java Program to Perform Addition Operation Using Bitwise Operators