Class StorageBasedLockProvider

java.lang.Object
net.javacrumbs.shedlock.support.StorageBasedLockProvider
All Implemented Interfaces:
ExtensibleLockProvider, LockProvider

public class StorageBasedLockProvider extends Object implements ExtensibleLockProvider
Distributed lock using abstract storage

It uses a table/collection that contains ID = lock name and a field locked_until.

  1. Attempts to insert a new lock record. As an optimization, we keep in-memory track of created lock records. If the record has been inserted, returns lock.
  2. We will try to update lock record using filter ID == name AND lock_until <= now
  3. If the update succeeded (1 updated row/document), we have the lock. If the update failed (0 updated documents) somebody else holds the lock
  4. When unlocking, lock_until is set to now.
  • Constructor Details

    • StorageBasedLockProvider

      protected StorageBasedLockProvider(StorageAccessor storageAccessor)
  • Method Details

    • clearCache

      public void clearCache()
      Clears cache of existing lock records.
    • lock

      public Optional<SimpleLock> lock(LockConfiguration lockConfiguration)
      Specified by:
      lock in interface LockProvider
      Returns:
      If empty optional has been returned, lock could not be acquired. The lock has to be released by the callee.
    • doLock

      protected boolean doLock(LockConfiguration lockConfiguration)
      Sets lockUntil according to LockConfiguration if current lockUntil <= now