Class DefaultLockManager

java.lang.Object
org.tentackle.persist.lock.DefaultLockManager
All Implemented Interfaces:
LockManager

@Service(LockManager.class) public class DefaultLockManager extends Object implements LockManager
Default implementation of LockManager.

The token locks are persisted in an extra table of DbTokenLocks in parallel to the token-lock columns of the PDOs. Additionally, the locks are kept in memory along with the name of the domain context they were created in. Changes within transactions are tracked and reverted on rollback.

Applications may keep certain token locks on session cleanup or server startup by overriding isTokenLockToKeep(DbTokenLock, DomainContext).

  • Constructor Details

    • DefaultLockManager

      public DefaultLockManager()
      Creates the lock manager.
      Enabled only if running within a server application connected directly to the database.
  • Method Details

    • isEnabled

      public boolean isEnabled()
      Description copied from interface: LockManager
      Returns whether the lock manager is enabled.
      Specified by:
      isEnabled in interface LockManager
      Returns:
      true if running in a server connected directly to the database
    • cleanupUserTokens

      public void cleanupUserTokens(Db session, long userId)
      Removes all tokens for a given user.
      The method is invoked when a user logs out or its session crashed.

      Token locks for which isTokenLockToKeep(DbTokenLock, DomainContext) returns true are kept. Tokens taken over by another user in the meantime are not removed.

      Specified by:
      cleanupUserTokens in interface LockManager
      Parameters:
      session - the session to use
      userId - the user ID
    • initialize

      public void initialize(Db session)
      Initializes the lock manager.
      Called once at server startup to remove all stale locks.

      Token locks for which isTokenLockToKeep(DbTokenLock, DomainContext) returns true are kept as tainted locks, since their domain context is unknown.

      Specified by:
      initialize in interface LockManager
      Parameters:
      session - the session to use
    • getLock

      public <T extends PersistentDomainObject<T>> TokenLock getLock(T pdo)
      Description copied from interface: LockManager
      Gets the token lock for a PDO.
      Specified by:
      getLock in interface LockManager
      Type Parameters:
      T - the PDO type
      Parameters:
      pdo - the PDO
      Returns:
      the token lock, null if PDO is not locked
    • getLocks

      public Collection<TokenLock> getLocks(long userId)
      Description copied from interface: LockManager
      Gets the token locks for a user.
      Specified by:
      getLocks in interface LockManager
      Parameters:
      userId - the user's ID
      Returns:
      the token locks
    • getLocks

      public Collection<TokenLock> getLocks()
      Description copied from interface: LockManager
      Gets all token locks.
      Specified by:
      getLocks in interface LockManager
      Returns:
      the token locks
    • update

      public <T extends PersistentDomainObject<T>> void update(T pdo)
      Updates the token lock.
      The method is invoked from within a transaction after the PDO has been persisted successfully.

      Creates, updates or removes the DbTokenLock according to the token-lock attributes of the PDO. If an unexpected exception occurs, the internal map is rebuilt from the database as soon as there are no more transactions in flight.

      Specified by:
      update in interface LockManager
      Type Parameters:
      T - the PDO type
      Parameters:
      pdo - the PDO
      Throws:
      LockException - if the lock was created within a domain context the PDO's context does not belong to
    • rollback

      public void rollback(Db session, long txNumber)
      Notifies the lock manager that a transaction has been rolled back.

      Reverts all lock changes made within the transaction.

      Specified by:
      rollback in interface LockManager
      Parameters:
      session - the session
      txNumber - the unique transaction number
    • commit

      public void commit(Db session, long txNumber)
      Description copied from interface: LockManager
      Notifies the lock manager that a transaction has been committed.
      Specified by:
      commit in interface LockManager
      Parameters:
      session - the session
      txNumber - the unique transaction number
    • createTokenLock

      protected DbTokenLock createTokenLock(Db session)
      Creates a token lock persistent object.
      Parameters:
      session - the session
      Returns:
      the PO
    • isTokenLockToKeep

      protected boolean isTokenLockToKeep(DbTokenLock dbTokenLock, DomainContext context)
      Returns whether a token lock should be kept on session cleanup or after server start.
      The default implementation returns false.
      Parameters:
      dbTokenLock - the token lock
      context - the domain context to use
      Returns:
      true to keep it
    • reloadLocks

      protected void reloadLocks(Db session)
      Reloads locks from the database into the internal map.
      Invoked to rebuild the map after an unexpected error in update(PersistentDomainObject). There must be no transactions running, and the caller must hold the monitor of the internal map!

      The context names are restored from the old map, if possible. Otherwise, the locks are marked as tainted.

      Parameters:
      session - the session