Package org.h2.store

Class 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

      Fields 
      Modifier and Type Field Description
      private static java.lang.String FILE  
      private java.lang.String fileName
      The lock file name.
      private long lastWrite
      The last time the lock file was written.
      private boolean locked
      Whether the file is locked.
      private static java.lang.String MAGIC  
      private java.lang.String method  
      private java.util.Properties properties  
      private static int RANDOM_BYTES  
      private java.net.ServerSocket serverSocket
      The server socket (only used when using the SOCKET mode).
      private int sleep
      The number of milliseconds to sleep after checking a file.
      private static int SLEEP_GAP  
      private static java.lang.String SOCKET  
      private static int TIME_GRANULARITY  
      private Trace trace
      The trace object.
      private java.lang.String uniqueId  
      private java.lang.Thread watchdog  
    • Constructor Summary

      Constructors 
      Constructor Description
      FileLock​(TraceSystem traceSystem, java.lang.String fileName, int sleep)
      Create a new file locking object.
    • Field Detail

      • 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 use
        fileName - the file name
        sleep - 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 key
        value - 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:
        run in interface java.lang.Runnable