Top Description Fields Constructors Methods
org.h2.store

public Class FileLock

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Imports
java.io.IOException, .OutputStream, java.net.BindException, .ConnectException, .InetAddress, .ServerSocket, .Socket, .UnknownHostException, java.nio.ByteBuffer, java.nio.channels.FileChannel, java.util.Properties, org.h2.Driver, org.h2.api.ErrorCode, org.h2.engine.Constants, .SessionRemote, org.h2.message.DbException, .Trace, .TraceSystem, org.h2.store.fs.FilePath, .FileUtils, org.h2.util.MathUtils, .NetUtils, .SortedProperties, .StringUtils, org.h2.value.Transfer

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

Modifier and TypeField and Description
private static final String
private volatile String
fileName

The lock file name.

private long
lastWrite

The last time the lock file was written.

private volatile boolean
locked

Whether the file is locked.

private static final String
private String
private Properties
private static final int
private volatile ServerSocket
serverSocket

The server socket (only used when using the SOCKET mode).

private final int
sleep

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
trace

The trace object.

private String
private Thread

Constructor Summary

AccessConstructor and Description
public
FileLock(TraceSystem
the trace system to use
traceSystem
,
String
the file name
fileName
,
int
the number of milliseconds to sleep
sleep
)

Create a new file locking object.

Method Summary

Modifier and TypeMethod and Description
private static long

Returns:

last modified date/time in milliseconds UTC
aggressiveLastModified
(String
file name to check
fileName
)

Aggressively read last modified time, to work-around remote filesystems.

private void
private DbException
private static DbException
public static FileLockMethod

Returns:

the method type
getFileLockMethod
(String
the method name
method
)

Get the file locking method type given a method name.

public String
public Properties

Returns:

the properties
load
()

Load the properties file.

public synchronized void
lock(FileLockMethod
the file locking method to use
fileLockMethod
)

Lock the file if possible.

private void
private void
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

public Properties

Returns:

the saved properties
save
()

Save the lock file.

public void
setProperty(String
the key
key
,
String
the value
value
)

Add or change a setting to the properties.

private void
private static void
sleep(int time)

public synchronized void
unlock()

Unlock the file.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FILEback to summary
private static final String FILE
fileNameback to summary
private volatile String fileName

The lock file name.

lastWriteback to summary
private long lastWrite

The last time the lock file was written.

lockedback to summary
private volatile boolean locked

Whether the file is locked.

MAGICback to summary
private static final String MAGIC
methodback to summary
private String method
propertiesback to summary
private Properties properties
RANDOM_BYTESback to summary
private static final int RANDOM_BYTES
serverSocketback to summary
private volatile ServerSocket serverSocket

The server socket (only used when using the SOCKET mode).

sleepback to summary
private final int sleep

The number of milliseconds to sleep after checking a file.

SLEEP_GAPback to summary
private static final int SLEEP_GAP
SOCKETback to summary
private static final String SOCKET
TIME_GRANULARITYback to summary
private static final int TIME_GRANULARITY
traceback to summary
private final Trace trace

The trace object.

uniqueIdback to summary
private String uniqueId
watchdogback to summary
private Thread watchdog

Constructor Detail

FileLockback to summary
public FileLock(TraceSystem traceSystem, String fileName, int sleep)

Create a new file locking object.

Parameters
traceSystem:TraceSystem

the trace system to use

fileName:String

the file name

sleep:int

the number of milliseconds to sleep

Method Detail

aggressiveLastModifiedback to summary
private static long aggressiveLastModified(String fileName)

Aggressively read last modified time, to work-around remote filesystems.

Parameters
fileName:String

file name to check

Returns:long

last modified date/time in milliseconds UTC

checkServerback to summary
private void checkServer()
getExceptionAlreadyInUseback to summary
private DbException getExceptionAlreadyInUse(String reason)
getExceptionFatalback to summary
private static DbException getExceptionFatal(String reason, Throwable t)
getFileLockMethodback to summary
public static FileLockMethod getFileLockMethod(String method)

Get the file locking method type given a method name.

Parameters
method:String

the method name

Returns:FileLockMethod

the method type

Exceptions
DbException:
if the method name is unknown
getUniqueIdback to summary
public String getUniqueId()
loadback to summary
public Properties load()

Load the properties file.

Returns:Properties

the properties

lockback to summary
public synchronized void lock(FileLockMethod fileLockMethod)

Lock the file if possible. A file may only be locked once.

Parameters
fileLockMethod:FileLockMethod

the file locking method to use

Exceptions
DbException:
if locking was not successful
lockFileback to summary
private void lockFile()
lockSocketback to summary
private void lockSocket()
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
saveback to summary
public Properties save()

Save the lock file.

Returns:Properties

the saved properties

setPropertyback to summary
public void setProperty(String key, String value)

Add or change a setting to the properties. This call does not save the file.

Parameters
key:String

the key

value:String

the value

setUniqueIdback to summary
private void setUniqueId()
sleepback to summary
private static void sleep(int time)
unlockback to summary
public synchronized void unlock()

Unlock the file. The watchdog thread is stopped. This method does nothing if the file is already unlocked.

waitUntilOldback to summary
private void waitUntilOld()