Database Reference
In-Depth Information
+ "\t" + book.getLanguage());
book1.setLanguage(new HashSet<String>(
Arrays.asList("English", "German", "Latin")));
dynamoDBMapper.save(book1);
dynamoDBMapper.save(book);
} catch (ConditionalCheckFailedException e) {
System.out.println(e.getMessage());
}
It will print the following error message:
The conditional request failed (Service: AmazonDynamoDBv2;
Status Code: 400; Error Code:
ConditionalCheckFailedException;
In the previous code block, what we do is this: first, we retrieve BookEntity with the
key values Hadoop and 2012-12-28 , which will fetch the version attribute (let's say
that the value is 1 ). Then, after printing the entity parameters, we update the item by set-
ting the Language set to "English", "German" .
Second, we again retrieve BookEntity with the key values Hadoop and
2012-12-28 , which will fetch the version attribute too (the value will remain 1 , be-
cause no write operation has been performed yet on the item). Again, we print the entity
parameters, and then we update the item by setting the Language set to "English",
"German", "Latin" . Then we try to save the second retrieved item. This item will be
saved (or updated) without any error, because the version attribute value still remains 1 .
This update operation will increment the version attribute value by 1 . So, its value
changes to 2 , so in the next line, we save the first updated item. This will throw the
ConditionalCheckFailedException exception, because during the retrieval of
the item, the version attribute value was 1 , but now (due to the last update operation) its
value has become 2 . This clearly means that DynamoDBMapper is trying to write an ob-
solete item, which is no longer allowed.
If we want to disable this optimistic locking, we can rewrite our block with the following
code:
dynamoDBMapper.save(book1, new DynamoDBMapperConfig(
DynamoDBMapperConfig.SaveBehavior.CLOBBER));
Search WWH ::




Custom Search