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()
.async()
.getAndLock(key, getLockTime())
.retryWhen(getRetryFunction());

After Document serialized CAS value printed - 1598622635610406912

Below is code to upsert updated document -
getBucket()
.async()
.upsert(doc, getPersistTo(),
getReplicateTo())
.retryWhen(getRetryFunction());

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

com.couchbase.client.java.error.CASMismatchException: null
at com.couchbase.client.java.bucket.api.Mutate$2$1.call(Mutate.java:243)
at com.couchbase.client.java.bucket.api.Mutate$2$1.call(Mutate.java:220)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at rx.observers.Subscribers$5.onNext(Subscribers.java:235)
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onNext(OnSubscribeDoOnEach.java:101)
at rx.internal.producers.SingleProducer.request(SingleProducer.java:65)
at rx.internal.producers.ProducerArbiter.setProducer(ProducerArbiter.java:126)
at rx.internal.operators.OnSubscribeRedo$2$1.setProducer(OnSubscribeRedo.java:267)

Please suggest what I’m missing here.

Regards,
N N

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.