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 Find Number of Spanning Trees in a Complete Bipartite Graph
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement Caesar Cypher
ETL with Spring Cloud Data Flow
Convert String to Byte Array and Reverse in Java
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Hướng dẫn Java Design Pattern – Factory Method
Quick Guide to Spring MVC with Velocity
Spring Boot - Enabling HTTPS
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Implement Find all Cross Edges in a Graph
Marker Interface trong Java
Performance Difference Between save() and saveAll() in Spring Data
Functional Interface trong Java 8
Java Program to Implement Trie
Java Program to Implement HashTable API
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Hướng dẫn sử dụng Java Generics
Java Program to Implement Hash Tree
Hướng dẫn Java Design Pattern – Visitor
A Guide to JPA with Spring
How to Store Duplicate Keys in a Map in Java?
Adding Parameters to HttpClient Requests
Java Program to Generate Random Numbers Using Multiply with Carry Method
Converting between an Array and a List in Java
Jackson Ignore Properties on Marshalling
RestTemplate Post Request with JSON
Convert XML to JSON Using Jackson
Java Program to Check if a Matrix is Invertible
A Guide to Apache Commons Collections CollectionUtils
Java Program to Find a Good Feedback Edge Set in a Graph