Locks
Enforce mutual exclusion on cached objects.
This is a legacy Apache Ignite documentationThe new documentation is hosted here: https://ignite.apache.org/docs/latest/
Cache transactions will acquire locks implicitly. However, there are cases when explicit locks are more useful. The lock() method of the IgniteCache API returns an instance of java.util.concurrent.locks.Lock that lets you define explicit distributed locks for any given key. Locks can also be acquired on a collection of objects using the IgniteCache.lockAll() method.
IgniteCache<String, Integer> cache = ignite.cache("myCache");
// Create a lock for the given key
Lock lock = cache.lock("keyLock");
try {
// Acquire the lock
lock.lock();
cache.put("Hello", 11);
cache.put("World", 22);
}
finally {
// Release the lock
lock.unlock();
}
Atomicity ModeIn Ignite, locks are supported only for
TRANSACTIONALatomicity mode, which can be configured via theatomicityModeproperty ofCacheConfiguration.
Locks and Transactions
Explicit locks are not transactional and cannot not be used from within transactions (exception will be thrown). If you do need explicit locking within transactions, then you should use TransactionConcurrency.PESSIMISTIC concurrency control for transactions which will acquire explicit locks for relevant cache operations.
Updated 11 months ago
