【发布时间】:2020-08-12 13:44:48
【问题描述】:
我有一个对象,SomeObject,它表示作为文档存储在 Couchbase 中的对象。 SomeObject 有一个用于包含 CAS 值的 cas 变量。
我有这样的代码:
/* Get two identical objects from Couchbase, they'll have identical CAS value */
SomeObject someObjectA = getSomeObjectFromCouchbase(sameId);
SomeObject someObjectB = getSomeObjectFromCouchbase(sameId);
/* Make arbitrary modifications to the objects */
someObjectA.getListInObject().add(arbitraryValue1);
someObjectB.getListInObject().add(arbitraryValue2);
/* Convert SomeObject objects to JsonDocument objects, ensuring the CAS value is set */
JsonDocument jsonDocA = JsonDocument.create(someObjectA.getId(), JsonObject.fromJson(mapper.writeValueAsString(someObjectA)), someObjectA.getCas());
JsonDocument jsonDocB = JsonDocument.create(someObjectB.getId(), JsonObject.fromJson(mapper.writeValueAsString(someObjectB)), someObjectB.getCas());
/* Perform upserts on both JsonDocument objects; expectation is the second one should fail with CASMismatchException because the CAS value should have changed after the first upsert */
couchbaseDao.getDatasource().getBucket().upsert(jsonDocA, writeTimeout, TimeUnit.MILLISECONDS);
couchbaseDao.getDatasource().getBucket().upsert(jsonDocB, writeTimeout, TimeUnit.MILLISECONDS);
尽管我期望第二个 upsert 应该失败并出现 CASMismatchException,我试图通过将代码包装在 try/catch 块中来捕获它,但它并没有发生。两次 upsert 都成功,并且服务器确实在两次 upsert 之后更改了 CAS 值。就好像它在 upsert 时甚至没有检查 CAS 值,只是盲目地接受任何东西然后更新 CAS 值。
最终结果是 Couchbase 文档中的列表仅包含任意值 2,并且缺少任意值 1,而我希望它具有任意值 1 而不是任意值 2(因为第二个 upsert 应该抛出 CASMismatchException)。是我做错了什么,还是服务器出了问题,无法正确处理 CAS?
【问题讨论】:
标签: java couchbase concurrentmodification