java dns 缓存_禁用Java DNS缓存-Disable DNS caching

Java应用程序在进行网络访问后,DNS设置会被缓存,导致后续操作使用旧设置,即使实际设置已更改。要重置DNS缓存,通常需要重启服务器。可以通过设置四个属性来覆盖默认行为,例如-Dsun.net.inetaddr.ttl=0,以在启动JVM时禁用缓存。同时,可以在java.security文件中修改networkaddress.cache.ttl和networkaddress.cache.negative.ttl的值来调整缓存策略。
摘要由CSDN通过智能技术生成

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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值