org.exist.storage.lock
Class FileLock

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.