Database Reference
In-Depth Information
PutItemRequest("ProductTable", item);
// Here we want DynamoDB to update the record only when its
value is 2
AttributeValue itemCounter = new
AttributeValue().withS("2");
Map<String,ExpectedAttributeValue> expected = new
HashMap<String,ExpectedAttributeValue>();
expected.put("data", new
ExpectedAttributeValue().withValue(itemCounter));
putItemRequest.setExpected(expected);
//Invoke the request
PutItemResult putItemResult =
ddbClient.putItem(putItemRequest);
If the value present in a DynamoDB table does not match with the expected value, it
throws ConditionalCheckFailedException ; it is always a good practice to
catch an exception, try to fetch the current value in DynamoDB, and then retry the request
as shown in the following code:
try {
// Invoke request
PutItemResult putItemResult =
ddbClient.putItem(putItemRequest);
}
catch (ConditionalCheckFailedException error) {
// Conditional write failed because of unexpected value
in DynamoDB
// Fetch the current value and retry the request
}
Search WWH ::




Custom Search