brad12s
brad12s

Reputation: 113

canonical file path in java - optimization problem?

My file structure has a symbolic link to a directory /home/me/myDir -> /some/other/dir. This link gets updated by another process and the notifies my process. Upon notification I attempt to get the new canonical path:

public static String getPath()
{
   File file = new File("/home/me/myDir");
   if(file.exists())
   {
      try
      {
         String canonical = file.getCanonicalPath();
         return canonical;
      }
      catch ...
   }

}

The problem is that after the link is changed (an i have verified it changes) it is taking 3-5 times of calling the above getPath() method for to actually get the new path before that the previous path is returned. The only thing I can think of is that java might be optimizing this method and returning the old path. Any ideas or insight is greatly appreciated.

Upvotes: 9

Views: 8219

Answers (1)

prunge
prunge

Reputation: 23268

Try disabling Java's canonicalization cache. This can be done by setting the system properties sun.io.useCanonCaches and sun.io.useCanonPrefixCache to false.

By default, canonical file names are cached for 30 seconds (read from source here).

Upvotes: 16

Related Questions