Class DefaultLockManager
- All Implemented Interfaces:
LockManager
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 Summary
ConstructorsConstructorDescriptionCreates the lock manager.
Enabled only if running within a server application connected directly to the database. -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanupUserTokens(Db session, long userId) Removes all tokens for a given user.
The method is invoked when a user logs out or its session crashed.voidNotifies the lock manager that a transaction has been committed.protected DbTokenLockcreateTokenLock(Db session) Creates a token lock persistent object.<T extends PersistentDomainObject<T>>
TokenLockgetLock(T pdo) Gets the token lock for a PDO.getLocks()Gets all token locks.getLocks(long userId) Gets the token locks for a user.voidinitialize(Db session) Initializes the lock manager.
Called once at server startup to remove all stale locks.booleanReturns whether the lock manager is enabled.protected booleanisTokenLockToKeep(DbTokenLock dbTokenLock, DomainContext context) Returns whether a token lock should be kept on session cleanup or after server start.
The default implementation returns false.protected voidreloadLocks(Db session) Reloads locks from the database into the internal map.
Invoked to rebuild the map after an unexpected error inupdate(PersistentDomainObject).voidNotifies the lock manager that a transaction has been rolled back.<T extends PersistentDomainObject<T>>
voidupdate(T pdo) Updates the token lock.
The method is invoked from within a transaction after the PDO has been persisted successfully.
-
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:LockManagerReturns whether the lock manager is enabled.- Specified by:
isEnabledin interfaceLockManager- Returns:
- true if running in a server connected directly to the database
-
cleanupUserTokens
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:
cleanupUserTokensin interfaceLockManager- Parameters:
session- the session to useuserId- the user ID
-
initialize
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:
initializein interfaceLockManager- Parameters:
session- the session to use
-
getLock
Description copied from interface:LockManagerGets the token lock for a PDO.- Specified by:
getLockin interfaceLockManager- Type Parameters:
T- the PDO type- Parameters:
pdo- the PDO- Returns:
- the token lock, null if PDO is not locked
-
getLocks
Description copied from interface:LockManagerGets the token locks for a user.- Specified by:
getLocksin interfaceLockManager- Parameters:
userId- the user's ID- Returns:
- the token locks
-
getLocks
Description copied from interface:LockManagerGets all token locks.- Specified by:
getLocksin interfaceLockManager- Returns:
- the token locks
-
update
Updates the token lock.
The method is invoked from within a transaction after the PDO has been persisted successfully.Creates, updates or removes the
DbTokenLockaccording 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:
updatein interfaceLockManager- 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
Notifies the lock manager that a transaction has been rolled back.Reverts all lock changes made within the transaction.
- Specified by:
rollbackin interfaceLockManager- Parameters:
session- the sessiontxNumber- the unique transaction number
-
commit
Description copied from interface:LockManagerNotifies the lock manager that a transaction has been committed.- Specified by:
commitin interfaceLockManager- Parameters:
session- the sessiontxNumber- the unique transaction number
-
createTokenLock
Creates a token lock persistent object.- Parameters:
session- the session- Returns:
- the PO
-
isTokenLockToKeep
Returns whether a token lock should be kept on session cleanup or after server start.
The default implementation returns false.- Parameters:
dbTokenLock- the token lockcontext- the domain context to use- Returns:
- true to keep it
-
reloadLocks
Reloads locks from the database into the internal map.
Invoked to rebuild the map after an unexpected error inupdate(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
-