Class InMemoryLockManager

java.lang.Object
de.calamanari.pk.coarsegrainedlock.InMemoryLockManager

public final class InMemoryLockManager
extends java.lang.Object
In-Memory Lock Manager - manages locks in the COARSE GRAINED LOCK example
In non-distributed scenarios it often makes more sense to leverage an in-memory lock management rather than storing lock information in database tables.
A big advantage (besides performance) of in-memory management is the fact that the system is always in a clean state after startup (no lock zombies surviving in lock tables).
Author:
Karl Eilebrecht
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static class  InMemoryLockManager.ElementLock
    Implementation of an immutable element lock with metadata and option to wait for release
    static class  InMemoryLockManager.LockType
    Enumeration of supported lock types
  • Method Summary

    Modifier and Type Method Description
    static boolean acquireReadLock​(java.lang.String elementId, java.lang.String ownerId)
    Creates a read lock for the given owner
    static boolean acquireWriteLock​(java.lang.String elementId, java.lang.String ownerId)
    Creates a write lock for the given owner
    static InMemoryLockManager.ElementLock getLockInfo​(java.lang.String elementId)
    Returns the lock currently associated with the specified element
    static boolean releaseLock​(java.lang.String elementId, java.lang.String ownerId)
    Releases the lock the given owner has on the specified element

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • acquireReadLock

      public static boolean acquireReadLock​(java.lang.String elementId, java.lang.String ownerId)
      Creates a read lock for the given owner
      Parameters:
      elementId - global unique identifier of the item to be locked
      ownerId - requester of the lock
      Returns:
      true if lock was set otherwise false
    • acquireWriteLock

      public static boolean acquireWriteLock​(java.lang.String elementId, java.lang.String ownerId)
      Creates a write lock for the given owner
      Parameters:
      elementId - global unique identifier of the item to be locked
      ownerId - requester of the lock
      Returns:
      true if lock was set otherwise false
    • releaseLock

      public static boolean releaseLock​(java.lang.String elementId, java.lang.String ownerId)
      Releases the lock the given owner has on the specified element
      Parameters:
      elementId - global unique identifier of the item to be locked
      ownerId - requester of the lock
      Returns:
      true if lock was released, otherwise false (no lock found)
    • getLockInfo

      public static InMemoryLockManager.ElementLock getLockInfo​(java.lang.String elementId)
      Returns the lock currently associated with the specified element
      Parameters:
      elementId - globally unique identifier
      Returns:
      lock meta data object or null if no lock was found