Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java doesn't pick up system's DNS settings change until restarted

843790Mar 6 2008 — edited Mar 28 2008
Hello,

I have a service running on a few Linux computers. Those computers have a NIC, which is configured with a fixed IP address. So /etc/resolv.conf contains the IP address of the LAN's DNS server. But most of the time, the computers are not plugged into the LAN at all. Instead, they connect themselves periodically to the Internet by establishing a ppp connection. When it happens, the ISP's DHCP server assign them new IP parameters, including their DNS server's IP address. And /etc/resolv.conf gets updated with this address.

But it seems that java doesn't take care of DNS change taking place during a run. Which means that my program (started at boot with no connectivity at all) tries to connect to some host, and obviously trigger an "UnknownHostException" (at that point it does try to contact the LAN's DNS server). Quite logical. Later, the ppp link become active. But my program still try to contact the LAN's DNS server, despite the new configuration in /etc/resolv.conf. As such, it will forever trigger UnknowHostExceptions, until it gets restarted (it will then pick up the new settings) or it is plugged back into the LAN (it will finally reach the LAN's DNS server).

This is quite a problem as during one single execution, the machine may encounter several DNS configuration changes, and this problem basically means that it will be impossible for my application to resolve any name at all.

So is there a way to tell Java to re-read the system wide DNS configuration? Or is there some option to prevent Java to "cache" the DNS server to use?

To demonstrate my problem, I've written a simple test case, see below.

To get the problem:

1) Put a bogus DNS server into your /etc/resolv.conf
2) Start the test program. Wait for some time: it will trigger UnknownHostExceptions.
3) Fix the entry in /etc/resolv.conf, and check it actually works (eg ping www.google.be)
4) Test program will continue to trigger UnknownHostExceptions forever.

One interesting fact is that someone tried this test on Windows, and didn't suffer from this behaviour, eg the application reacts to DNS system settings changes dynamically. So it looks like a Linux-only problem.

Thanks in advance for your insight.
Pierre-Yves
package com.test.dnsresolver;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class DnsResolver {
 
    private static String urlString = "www.google.com";
 
    public static void main(String[] args) {
 
        /**
         * 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.
         */
        java.security.Security.setProperty("networkaddress.cache.ttl" , "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".
         */
        java.security.Security.setProperty("networkaddress.cache.negative.ttl", "0");
         
        int loopCounter = 0;
        while (true) {
            InetAddress resolved;
            try {
                resolved = InetAddress.getByName(urlString);
                System.out.println("Loop " + loopCounter + ": resolved IP address: " + resolved);
            } catch (UnknownHostException e) {
                System.out.println("Loop " + loopCounter + ": UnknownHostException");
                e.printStackTrace();
            }
            loopCounter++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
        }
         
    }
 
}

Comments

fac586

Richard Legge wrote:

Hi all, this mornings question.. please let it be a nice simple answer...

Im using APEX 4.2, and have switch to theme 15. simple blue.

Ive got a menu list on the left hand side. so it needs to sit in region 2. its designated as a sidebar region.   I'm trying to use the sidebar template so that I get a menu header and a border... However region 2 on this theme has a blue background... and also has a header and footer area / image.

So my sidebar region looks odd inside it.

From my research, I should be able to override the region template to remove this header / footer / background colour and associated attributes, leaving me with a nice sidebar menu... Ive don lots of research, and think I should be referencing the region class in the definition of my menu??? but dont seem to be able to get it right..

Please could somebody help

Somebody would be able to help more quickly and effectively if they had an example on apex.oracle.com to refer to...

However, if you're using 4.2 and not supporting legacy (IE6/7) browsers I would recommend not using theme 15, nor any of the other legacy quirks mode themes (marked with a "*"). You'll find it much easier to work with—and get support for—the more modern standards-based themes.

Richard Legge

Have I missed something then, as Ive only got two standard themes.. 23 (uniframe) and 26 (productivity Apps).. It doesn't give me much to work with... are there others available?

Thanks

Richard

fac586

Richard Legge wrote:

Have I missed something then, as Ive only got two standard themes.. 23 (uniframe) and 26 (productivity Apps).. It doesn't give me much to work with... are there others available?

You must have some of the "Standard" themes already installed in the app.

When you go through the Create Theme wizard, on the Identify Theme page, select All Themes from the Theme Type select list, and use any of them except those marked with a "*": 8, 10, 11, 13 (which is not marked but should be), 14, 15, 16, 18, 19, and 20.

All of the other themes (regardless of their 4.2 labelling as "Standard" or "Legacy") contain a DOCTYPE declaration that will trigger standards mode in browsers.

Richard Legge

ahh.  Thanks. Yes, forgot that if Id already imported them, they dont show up on the list.. many thanks

1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 25 2008
Added on Mar 6 2008
10 comments
10,139 views