How to handle CASMismatchException by changing CAS value?


#1

Hi,

I am testing my Java/Spring micro-service application which is using Couchbase. However, in my test scenario, if 2 threads try to update same my id (device_id in my Java class) (I think threads comes at the same time) my program gives exception which is :

org.springframework.dao.OptimisticLockingFailureException: Upsert document with version value failed: 1545056849924849664; nested exception is com.couchbase.client.java.error.CASMismatchException.

So if there is a logic for generating CAS value, how can I change it myself. I want to change cas value so 2 threads can perform update operation and my program does not give exception ? Any suggestion?


#2

Generally speaking, the way to handle OptimisticLockingFailureException is to retry the operation by loading the document, reapplying the modification, and saving again. That way you don’t lose any modifications made by the other thread.