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 implement Priority Queue
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Java Program to Encode a Message Using Playfair Cipher
Exploring the New Spring Cloud Gateway
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Spring Boot Configuration with Jasypt
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Implement Floyd Cycle Algorithm
LinkedList trong java
An Intro to Spring Cloud Contract
Check If a File or Directory Exists in Java
Notify User of Login From New Device or Location
Generate Spring Boot REST Client with Swagger
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Construct K-D Tree for 2 Dimensional Data
How to Get All Dates Between Two Dates?
Java Program to Perform the Shaker Sort
Java Program to Implement Regular Falsi Algorithm
Tips for dealing with HTTP-related problems
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to Implement LinkedHashMap API
Hướng dẫn Java Design Pattern – Iterator
Java – Combine Multiple Collections
Java Program to Implement HashTable API
Using Custom Banners in Spring Boot
Spring Data MongoDB – Indexes, Annotations and Converters
Giới thiệu Aspect Oriented Programming (AOP)
Hướng dẫn sử dụng String Format trong Java
Registration – Activate a New Account by Email
Using JWT with Spring Security OAuth (legacy stack)