Upsert is failing with CASMismatchException although CAS value is not changed

Hi all,

below is sample code to fetch locked document from Couchbase -
Observable ob = getBucket()
.getAndLock(key, getLockTime())

After Document serialized CAS value printed - 1598622635610406912

Below is code to upsert updated document -
.upsert(doc, getPersistTo(),

Before Upsert CAS value is printed - 1598622635610406912

I also checked on Couchbase query console -
SELECT meta(b).cas FROM filtering b WHERE meta(b).id = "XYZ:24"
result -
“cas”: 1598622635610406912

CAS value is same all time but still getting CASMismatchException null
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(
at rx.observers.Subscribers$5.onNext(
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onNext(
at rx.internal.producers.SingleProducer.request(
at rx.internal.producers.ProducerArbiter.setProducer(
at rx.internal.operators.OnSubscribeRedo$2$1.setProducer(

Please suggest what I’m missing here.


Hi @Naveen_Nisad

Using getAndLock means the document is pessimistically locked and requires a ‘key’ to unlock it - the key being the CAS returned from the getAndLock. So you need to call replace rather than upsert, which lets you supply the necessary CAS ‘key’.
Hope that helps.

1 Like

Thanks for fast reply, It worked well.