Package org.h2.store
Class FileLock
- java.lang.Object
-
- org.h2.store.FileLock
-
- All Implemented Interfaces:
java.lang.Runnable
public class FileLock extends java.lang.Object implements java.lang.RunnableThe 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
Fields Modifier and Type Field Description private static java.lang.StringFILEprivate java.lang.StringfileNameThe lock file name.private longlastWriteThe last time the lock file was written.private booleanlockedWhether the file is locked.private static java.lang.StringMAGICprivate java.lang.Stringmethodprivate java.util.Propertiespropertiesprivate static intRANDOM_BYTESprivate java.net.ServerSocketserverSocketThe server socket (only used when using the SOCKET mode).private intsleepThe number of milliseconds to sleep after checking a file.private static intSLEEP_GAPprivate static java.lang.StringSOCKETprivate static intTIME_GRANULARITYprivate TracetraceThe trace object.private java.lang.StringuniqueIdprivate java.lang.Threadwatchdog
-
Constructor Summary
Constructors Constructor Description FileLock(TraceSystem traceSystem, java.lang.String fileName, int sleep)Create a new file locking object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static longaggressiveLastModified(java.lang.String fileName)Aggressively read last modified time, to work-around remote filesystems.private voidcheckServer()private DbExceptiongetExceptionAlreadyInUse(java.lang.String reason)private static DbExceptiongetExceptionFatal(java.lang.String reason, java.lang.Throwable t)static FileLockMethodgetFileLockMethod(java.lang.String method)Get the file locking method type given a method name.java.lang.StringgetUniqueId()java.util.Propertiesload()Load the properties file.voidlock(FileLockMethod fileLockMethod)Lock the file if possible.private voidlockFile()private voidlockSocket()voidrun()java.util.Propertiessave()Save the lock file.voidsetProperty(java.lang.String key, java.lang.String value)Add or change a setting to the properties.private voidsetUniqueId()private static voidsleep(int time)voidunlock()Unlock the file.private voidwaitUntilOld()
-
-
-
Field Detail
-
MAGIC
private static final java.lang.String MAGIC
- See Also:
- Constant Field Values
-
FILE
private static final java.lang.String FILE
- See Also:
- Constant Field Values
-
SOCKET
private static final java.lang.String SOCKET
- See Also:
- Constant Field Values
-
RANDOM_BYTES
private static final int RANDOM_BYTES
- See Also:
- Constant Field Values
-
SLEEP_GAP
private static final int SLEEP_GAP
- See Also:
- Constant Field Values
-
TIME_GRANULARITY
private static final int TIME_GRANULARITY
- See Also:
- Constant Field Values
-
fileName
private volatile java.lang.String fileName
The lock file name.
-
serverSocket
private volatile java.net.ServerSocket serverSocket
The server socket (only used when using the SOCKET mode).
-
locked
private volatile boolean locked
Whether the file is locked.
-
sleep
private final int sleep
The number of milliseconds to sleep after checking a file.
-
trace
private final Trace trace
The trace object.
-
lastWrite
private long lastWrite
The last time the lock file was written.
-
method
private java.lang.String method
-
properties
private java.util.Properties properties
-
uniqueId
private java.lang.String uniqueId
-
watchdog
private java.lang.Thread watchdog
-
-
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 usefileName- the file namesleep- the number of milliseconds to sleep
-
-
Method Detail
-
lock
public void lock(FileLockMethod fileLockMethod)
Lock the file if possible. A file may only be locked once.- Parameters:
fileLockMethod- the file locking method to use- Throws:
DbException- 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 keyvalue- the value
-
save
public java.util.Properties save()
Save the lock file.- Returns:
- the saved properties
-
aggressiveLastModified
private static long aggressiveLastModified(java.lang.String fileName)
Aggressively read last modified time, to work-around remote filesystems.- Parameters:
fileName- file name to check- Returns:
- last modified date/time in milliseconds UTC
-
checkServer
private void checkServer()
-
load
public java.util.Properties load()
Load the properties file.- Returns:
- the properties
-
waitUntilOld
private void waitUntilOld()
-
setUniqueId
private void setUniqueId()
-
lockFile
private void lockFile()
-
lockSocket
private void lockSocket()
-
sleep
private static void sleep(int time)
-
getExceptionFatal
private static DbException getExceptionFatal(java.lang.String reason, java.lang.Throwable t)
-
getExceptionAlreadyInUse
private DbException getExceptionAlreadyInUse(java.lang.String reason)
-
getFileLockMethod
public static FileLockMethod 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:
DbException- if the method name is unknown
-
getUniqueId
public java.lang.String getUniqueId()
-
run
public void run()
- Specified by:
runin interfacejava.lang.Runnable
-
-