Class FileLock

  extended by

public class FileLock
extends Object

Cooperative inter-process file locking, used to synchronize access to database files across processes, i.e. across different Java VMs or separate database instances within one VM. This is similar to the native file locks provided by Java NIO. However, the NIO implementation has various problems. Among other things, we observed that locks were not properly released on WinXP. FileLock implements a cooperative approach. The class attempts to write a lock file at the specified location. Every lock file stores 1) a magic word to make sure that the file was really written by eXist, 2) a heartbeat timestamp. The procedure for acquiring the lock in tryLock() is as follows: If a lock file does already exist in the specified location, we check its heartbeat timestamp. If the timestamp is more than HEARTBEAT milliseconds in the past, we assume that the lock is stale and its owner process has died. The lock file is removed and we create a new one. If the heartbeat indicates that the owner process is still alive, the lock attempt is aborted and tryLock() returns false. Otherwise, we create a new lock file and start a daemon thread to periodically update the lock file's heart-beat value.

Wolfgang Meier

Constructor Summary
FileLock(BrokerPool pool, File parent, String lockName)
FileLock(BrokerPool pool, String path)
Method Summary
 File getFile()
          Returns the lock file that represents the active lock held by the FileLock.
 long getFreeSpace()
 Date getLastHeartbeat()
          Returns the last heartbeat written to the lock file.
 void release()
          Release the lock.
 boolean tryLock()
          Attempt to create the lock file and thus acquire a lock.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public FileLock(BrokerPool pool,
                String path)


public FileLock(BrokerPool pool,
                File parent,
                String lockName)
Method Detail


public boolean tryLock()
                throws ReadOnlyException
Attempt to create the lock file and thus acquire a lock.

false if another process holds the lock
ReadOnlyException - if the lock file could not be created or saved due to IO errors. The caller may want to switch to read-only mode.


public void release()
Release the lock. Removes the lock file and closes all open channels.


public Date getLastHeartbeat()
Returns the last heartbeat written to the lock file.

last heartbeat


public File getFile()
Returns the lock file that represents the active lock held by the FileLock.

lock file


public long getFreeSpace()

Copyright (C) The eXist-db Project. All rights reserved.