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:

Mapping Nested Values with Jackson
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Implement Gauss Jordan Elimination
Adding Shutdown Hooks for JVM Applications
Java Program to Perform Polygon Containment Test
Kết hợp Java Reflection và Java Annotations
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
RegEx for matching Date Pattern in Java
Java Program to Implement Pagoda
A Guide to JUnit 5
Java NIO2 Path API
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Compute DFT Coefficients Directly
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Introduction to Netflix Archaius with Spring Cloud
Java Program to Show the Duality Transformation of Line and Point
Java Program to Implement Tarjan Algorithm
A Custom Data Binder in Spring MVC
Programmatic Transaction Management in Spring
Class Loaders in Java
Convert String to Byte Array and Reverse in Java
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement LinkedBlockingDeque API
Java Program to find the number of occurrences of a given number using Binary Search approach
ETL with Spring Cloud Data Flow
An Introduction to ThreadLocal in Java
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement Best-First Search
Java Program to Implement Unrolled Linked List
So sánh HashMap và Hashtable trong Java
Spring Boot Integration Testing with Embedded MongoDB