|Modifier and Type||Field and Description|
|Constructor and Description|
|Modifier and Type||Method and Description|
Creates an advisory lock file having the same basename as the given file with a
Close the file and automatically releases the
Create/open the file and obtain an exclusive lock.
Removes the advisory lock for the file if it exists.
protected static final org.apache.log4j.Logger log
protected static final boolean INFO
protected static final boolean DEBUG
public static RandomAccessFile openFile(File file, String fileMode, boolean useFileLock) throws IOException
FileLock will be used when supported and requested. An advisory
lock will be used if useFileLock == false, if the fileMode
is read-only. If tryLock() returns
null then the lock
exists and this request will fail. However, if
FileChannel.tryLock() throws an
IOException then the
underlying platform does not support
FileLock for the named file
(memory mapped files, read-only files, and NFS mounted files can all have
this problem) and we will attempt to acquire an advisory lock instead.
Advisory locks are NOT visible to other applications
Do NOT request a
FileLock if you are going to use a memory-mapped
buffer. The JDK cautions that these things do not play well together on
file- The file.
fileMode- The file mode for
FileChannel.tryLock()should be attempted. when
falseonly an advisory lock will be sought.
IOException- If the file could not be opened or someone already holds a lock for that file.
public static void closeFile(File file, RandomAccessFile raf) throws IOException
FileLock(if any) and removes the advisory lock for that file (if any).
Note: This method should be used in combination with
openFile(File, String, boolean) in order to ensure that the
optional advisory lock file is deleted when the file is closed. The
purpose of the advisory lock file is to provide advisory locking file
modes (read-only), platforms, or file systems (NFS) that do not support
public static boolean acquireAdvisoryLock(File file) throws IOException
Note: This uses
File.createNewFile() which is NOT advised for
this purpose. However,
FileLock does not work in some contexts so
this is used as a fallback mechanism. We write a
UUID into the
advisory lock since Java does not have platform independent PIDs. That
UUID allows us to tell whether the advisory lock file was created
by this process or by another process.
Note: If a
Thread is interrupted during an NIO operation then the
FileChannel will be closed asynchronously. While this correctly
FileLock it does NOT cause our advisory lock file to
be deleted. During a normal shutdown of an
advisory lock file is deleted by
closeFile(File, RandomAccessFile). However, following an
abnormal shutdown the advisory lock file MAY still exist and (assuming
FileLock is not working since we created an advisory lock in
the first place) it MUST be removed by hand before the
AbstractJournal can be reopened.
public static boolean isOurLockFile(File lockFile) throws IOException
public static void removeAdvisoryLock(File file) throws IOException
Copyright © 2006–2016 SYSTAP, LLC DBA Blazegraph. All rights reserved.