How implement only Optimistic locking on document

We have implemented ‘CAS based optimistic locking’, that sounds very much a mix of Optimistic and Pessimistic resource locking.

Is there any way to implement only Optimistic lock only on Document?

Hi Naveen,

Sure, to do optimistic locking you:

  1. Read the document and remember the CAS value.
  2. Change the document and then issue a Replace request, passing the CAS value from step 1.
  3. If the replace fails with a CAS mismatch error, it means someone else modified the document after you read it, so go to step 1.

If this is what you’ve already implemented, then congrats, you’re doing it right :slight_smile:


Hi David

All three mentioned steps are the same in my implementation except when I’m reading Document using getAndLock(). I suppose, if I want to implement only Optimistic lock, I should use get() only instead of getAndLock()


1 Like