Java multiple parallel threads accessing getAndLock


#1

Hello,
I am working on a scenario where multiple threads try to access the same document (initially an empty document). I am loading an empty document to use getAndLock, but I am seeing while one thread gets the lock and processing the business logic to create the actual document, the other threads are receiving the TemporaryLockFailureException. Is there a way I can keep the other threads in the wait status until the first thread releases the lock?

Thanks


#2

If you want to block, you’ll have to implement that logic yourself: sleep for a few milliseconds then retry until you don’t receive the TemporaryLockFailure exception anymore…

Have you looked at and eliminated other options for your use case?
For example, most mutations can be done in an “optimistic locking” way using the CAS of the last known state. There is also atomic operations like append/prepend but that rules out having a JSON document (as it would append/prepend data beyond the boundary of the root object, eg. after the closing brackets for append)…


#3

Thank you for the prompt response. Using the other options the only time we need the optimistic locing using CAS if all the threads try to update the same document. But in my use case I want only the thread first acquires the lock will update the document and the remaining threads will get it from the cache store (readonly on the same key) once the first thread releases the lock.