Package org.h2.store
Class FileLock
java.lang.Object
org.h2.store.FileLock
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionprivate static final String
private String
The lock file name.private long
The last time the lock file was written.private boolean
Whether the file is locked.private static final String
private String
private Properties
private static final int
private ServerSocket
The server socket (only used when using the SOCKET mode).private final int
The number of milliseconds to sleep after checking a file.private static final int
private static final String
private static final int
private final Trace
The trace object.private String
private Thread
-
Constructor Summary
ConstructorsConstructorDescriptionFileLock
(TraceSystem traceSystem, String fileName, int sleep) Create a new file locking object. -
Method Summary
Modifier and TypeMethodDescriptionprivate static long
aggressiveLastModified
(String fileName) Aggressively read last modified time, to work-around remote filesystems.private void
private DbException
getExceptionAlreadyInUse
(String reason) private static DbException
getExceptionFatal
(String reason, Throwable t) static FileLockMethod
getFileLockMethod
(String method) Get the file locking method type given a method name.load()
Load the properties file.void
lock
(FileLockMethod fileLockMethod) Lock the file if possible.private void
lockFile()
private void
void
run()
save()
Save the lock file.void
setProperty
(String key, String value) Add or change a setting to the properties.private void
private static void
sleep
(int time) void
unlock()
Unlock the file.private void
-
Field Details
-
MAGIC
- See Also:
-
FILE
- See Also:
-
SOCKET
- See Also:
-
RANDOM_BYTES
private static final int RANDOM_BYTES- See Also:
-
SLEEP_GAP
private static final int SLEEP_GAP- See Also:
-
TIME_GRANULARITY
private static final int TIME_GRANULARITY- See Also:
-
fileName
The lock file name. -
serverSocket
The server socket (only used when using the SOCKET mode). -
locked
private volatile boolean lockedWhether the file is locked. -
sleep
private final int sleepThe number of milliseconds to sleep after checking a file. -
trace
The trace object. -
lastWrite
private long lastWriteThe last time the lock file was written. -
method
-
properties
-
uniqueId
-
watchdog
-
-
Constructor Details
-
FileLock
Create a new file locking object.- Parameters:
traceSystem
- the trace system to usefileName
- the file namesleep
- the number of milliseconds to sleep
-
-
Method Details
-
lock
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
Add or change a setting to the properties. This call does not save the file.- Parameters:
key
- the keyvalue
- the value
-
save
Save the lock file.- Returns:
- the saved properties
-
aggressiveLastModified
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
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
-
getExceptionAlreadyInUse
-
getFileLockMethod
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
-
run
public void run()
-