October 20th, Q&A session: Get you issues solved and questions answered!

GitHub logo

Distributed Locks


Ignite transactions acquire distributed locks implicitly. However, there are certain use cases when you might need to acquire the locks explicitly. 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

    cache.put("Hello", 11);
    cache.put("World", 22);
finally {
    // Release the lock

Atomicity Mode

In Ignite, locks are supported only for the TRANSACTIONAL atomicity mode, which can be set via the CacheConfiguration.atomicityMode parameter.

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 the TransactionConcurrency.PESSIMISTIC concurrency control for transactions which will acquire explicit locks for relevant cluster data requests.