org.h2.store
Class FileLock

java.lang.Object
  extended by org.h2.store.FileLock
All Implemented Interfaces:
java.lang.Runnable

public class FileLock
extends java.lang.Object
implements java.lang.Runnable

The file lock is used to lock a database so that only one process can write to it. It uses a cooperative locking protocol. Usually a .lock.db file is used, but locking by creating a socket is supported as well.


Field Summary
static int LOCK_FILE
          This locking method means the cooperative file locking protocol should be used.
static int LOCK_NO
          This locking method means no locking is used at all.
static int LOCK_SERIALIZED
          This locking method means multiple writers are allowed, and they synchronize themselves.
static int LOCK_SOCKET
          This locking method means a socket is created on the given machine.
 
Constructor Summary
FileLock(TraceSystem traceSystem, java.lang.String fileName, int sleep)
          Create a new file locking object.
 
Method Summary
static int getFileLockMethod(java.lang.String method)
          Get the file locking method type given a method name.
 java.lang.String getUniqueId()
           
 java.util.Properties load()
          Load the properties file.
 void lock(int fileLockMethod)
          Lock the file if possible.
 void run()
           
 java.util.Properties save()
          Save the lock file.
 void setProperty(java.lang.String key, java.lang.String value)
          Add or change a setting to the properties.
 void unlock()
          Unlock the file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCK_NO

public static final int LOCK_NO
This locking method means no locking is used at all.

See Also:
Constant Field Values

LOCK_FILE

public static final int LOCK_FILE
This locking method means the cooperative file locking protocol should be used.

See Also:
Constant Field Values

LOCK_SOCKET

public static final int LOCK_SOCKET
This locking method means a socket is created on the given machine.

See Also:
Constant Field Values

LOCK_SERIALIZED

public static final int LOCK_SERIALIZED
This locking method means multiple writers are allowed, and they synchronize themselves.

See Also:
Constant Field Values
Constructor Detail

FileLock

public FileLock(TraceSystem traceSystem,
                java.lang.String fileName,
                int sleep)
Create a new file locking object.

Parameters:
traceSystem - the trace system to use
fileName - the file name
sleep - the number of milliseconds to sleep
Method Detail

lock

public void lock(int fileLockMethod)
Lock the file if possible. A file may only be locked once.

Parameters:
fileLockMethod - the file locking method to use
Throws:
SQLException - if locking was not successful

unlock

public void unlock()
Unlock the file. The watchdog thread is stopped. This method does nothing if the file is already unlocked.


setProperty

public void setProperty(java.lang.String key,
                        java.lang.String value)
Add or change a setting to the properties. This call does not save the file.

Parameters:
key - the key
value - the value

save

public java.util.Properties save()
Save the lock file.

Returns:
the saved properties

load

public java.util.Properties load()
Load the properties file.

Returns:
the properties

getFileLockMethod

public static int getFileLockMethod(java.lang.String method)
Get the file locking method type given a method name.

Parameters:
method - the method name
Returns:
the method type
Throws:
SQLException - if the method name is unknown

getUniqueId

public java.lang.String getUniqueId()

run

public void run()
Specified by:
run in interface java.lang.Runnable