## org.exist.storage.lock
Class FileLock

    java.lang.Object
      org.exist.storage.lock.FileLock

public class FileLock
extends java.lang.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 heartbeat value.

Author:
  Wolfgang Meier

Constructor Summary
`FileLock(BrokerPool pool, java.io.File parent, java.lang.String lockName)`
`FileLock(BrokerPool pool, java.lang.String path)`

Method Summary
`java.io.File` `getFile()` Returns the lock file that represents the active lock held by the FileLock.
`java.util.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

### FileLock

    public FileLock(BrokerPool pool,
                    java.lang.String path)

### FileLock

    public FileLock(BrokerPool pool,
                    java.io.File parent,
                    java.lang.String lockName)

Method Detail

### tryLock

    public boolean tryLock()
                    throws ReadOnlyException

  Attempt to create the lock file and thus acquire a lock.

  Returns:
    false if another process holds the lock
  Throws:
    `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.

### release

    public void release()

  Release the lock. Removes the lock file and closes all open channels.

### getLastHeartbeat

    public java.util.Date getLastHeartbeat()

  Returns the last heartbeat written to the lock file.

  Returns:
    last heartbeat

### getFile

    public java.io.File getFile()

  Returns the lock file that represents the active lock held by the FileLock.

  Returns:
    lock file

Copyright (C) Wolfgang Meier. All rights reserved.