When you are using pessimistic and optimistic locking, shared (S), upgradeable (U) and exclusive (X) locks are used to maintain consistency. Understanding locking and its behavior is important when you have pessimistic locking enabled. With optimistic locking, the locks are not held. Different types of locks are compatible with others in various ways. Locks must be handled in the correct order to avoid deadlock scenarios.
In Java applications, locks
are also acquired when the applications uses the find methods on an
index, or does a query.
Lock mode (Java / .NET) | Java method equivalent | .NET method equivalent |
---|---|---|
SHARED / Shared | get and getAll methods on the ObjectMap interface, index methods, and queries | Get(), GetAndLock(Key,LockMode.Shared), GetAndLockAll(KeyList, LockMode.Shared),GetAll methods |
UPGRADABLE /Upgradable | getForUpdate(), getAllForUpdate() | GetAndLock(Key, LockMode.Updgradable), GetAndLockAll(KeyList, LockMode.Upgradable) |
EXCLUSIVE / Exclusive | getNextKey(), commit(), put, putAll, remove, removeAll, insert, update, and touch methods,
global invalidate and global invalidateAll methods. (No locks are acquired for the local invalidate and invalidateAll methods because none of the
BackingMap entries are invalidated by local invalidate method calls.)![]() ![]() ![]() |
Commit(), Add, AddAll,
Put, PutAll, Remove, RemoveAll, Replace, ReplaceAll, Touch, TouchAll,
Invalidate, InvalidateAll Note: The Put and PutAll methods are equivalent to the Java upsert and upsertAll methods.
|
The following table is a lock mode compatibility matrix that summarizes the described lock modes, which you can use to determine which lock modes are compatible with each other. To read this matrix, the row in the matrix indicates a lock mode that is already granted. The column indicates the lock mode that is requested by another transaction. If Yes is displayed in the column, then the lock mode that is requested by the other transaction is granted because it is compatible with the lock mode that is already granted. No indicates that the lock mode is not compatible and the other transaction must wait for the first transaction to release the lock that it owns.
Lock | Lock type S (shared) | Lock type U (upgradeable) | Lock type X (exclusive) | Strength |
---|---|---|---|---|
S (shared) | Yes | Yes | No | weakest |
U (upgradeable) | Yes | No | No | normal |
X (exclusive) | No | No | No | strongest |