0

I am using Playframework(scalaVersion 2.12.4) with Java 1.8 and Couchbase(Couchbase java-client 2.4.2). I am fetching UserDetails document and updating the same document in the database.

  • My code is working in normal scenarios, couldn't test the conflicting edge cases. Hence, not sure whether my code will work or not.
  • How to handle TemporaryLockFailureException? My current exception handling seems to be wrong.
@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();
}
kulsin
  • 398
  • 4
  • 18
  • Does this answer your question? [What is CAS in NoSQL and how to use it?](https://stackoverflow.com/questions/22601503/what-is-cas-in-nosql-and-how-to-use-it) – dnault Dec 13 '19 at 09:49
  • Instead of locking the document with `bucket.getAndLock` I'd recommend using optimistic locking with check-and-set (CAS). More info in the StackOverflow link above, and also in this blog post: [Optimistic or pessimistic locking – Which one should you pick?](https://blog.couchbase.com/optimistic-or-pessimistic-locking-which-one-should-you-pick/) – dnault Dec 13 '19 at 09:50

0 Answers0