How to handle CASMismatchException by changing CAS value?



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

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?


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.