我在Playframework
和scalaVersion 2.12.4
(Java 1.8
)中使用Couchbase
(Couchbase 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();
}