如何在沙发床数据库中锁定文档并处理TemporaryLockFailureException

时间:2019-11-29 18:40:52

标签: java exception playframework locking couchbase

我在PlayframeworkscalaVersion 2.12.4Java 1.8)中使用CouchbaseCouchbase java-client 2.4.2)。我正在获取UserDetails文档并更新数据库中的同一文档。

  • 我的代码在正常情况下运行,无法测试冲突的极端情况。因此,不确定我的代码是否可以工作。
  • 如何处理TemporaryLockFailureException?我当前的异常处理似乎是错误的。
@Override
public void submitUsersDetails(DashboardRequest request) {
    logger.debug("inside submitDashboardDetails");
    CompletableFuture.runAsync(() -> {
        try {
            bucket = cluster.openBucket(config.getString(PctConstants.COUCHBASE_BUCKET_NAME),
                    config.getString(PctConstants.COUCHBASE_BUCKET_PASSWORD));
            String documentID = PctConstants.USER_PROFILE;
            JsonDocument retrievedJsonDocument = bucket.getAndLock(documentID, 5);
            if (retrievedJsonDocument != null) {
                JsonNode json = Json.parse(retrievedJsonDocument.content().toString());
                UserDetails userDetails = Json.fromJson(json, UserDetails.class);
                // UPDATING userDetails
                JsonObject jsonObject = JsonObject.fromJson(Json.toJson(userDetails).toString());
                bucket.replace(JsonDocument.create(documentID, jsonObject, retrievedJsonDocument.cas()));
                logger.info("Successfully entered record into couchbase");
            } else {
                logger.error("Fetching User_details unsuccessful");
                throw new DataNotFoundException("User_details data not found");
            }
        } catch (TemporaryLockFailureException e) {
            try {
                logger.debug("Inside lock failure **************************");
                Thread.sleep(5000);
                String documentID = PctConstants.USER_PROFILE;
                JsonDocument retrievedJsonDocument = bucket.getAndLock(documentID, 5);
                if (retrievedJsonDocument != null) {
                    JsonNode json = Json.parse(retrievedJsonDocument.content().toString());
                    UserDetails userDetails = Json.fromJson(json, UserDetails.class);
                    // UPDATING userDetails
                    JsonObject jsonObject = JsonObject.fromJson(Json.toJson(userDetails).toString());
                    bucket.replace(JsonDocument.create(documentID, jsonObject, retrievedJsonDocument.cas()));
                    logger.info("Successfully entered record into couchbase");
                    submitDashboardDetails(request);
                } else {
                    logger.error("Fetching User_details unsuccessful");
                    throw new DataNotFoundException("User_details data not found");
                }
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                throw e;
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }, ec.current()).join();
}

0 个答案:

没有答案