aws-sdk-java-v2
aws-sdk-java-v2 copied to clipboard
Optimistic Locking while using Enhanced DDB Client.
Describe the issue
Need to upgrade SDK version to 2 for DDB client. I want to understand how to enforce optimistic locking while doing CRUD operation by enhanced DDB client. I am already using @DynamoDbVersionAttribute annotation.
Steps to Reproduce
- Insert item using TransactWriteItemsEnhancedRequest - PutItem.
- Fetch the item using table.getItem(GetItemEnhancedRequest). (recordVersion would be 1)
- Update one field of item using TransactWriteItemsEnhancedRequest - UpdateItem. (recordVersion will updated to 2 in the DDB.)
- Delete item using TransactWriteItemsEnhancedRequest - DeleteItem but use object fetched from Step 2 for deletion.
Current Behavior
Item is deleted successfully without any exception for record number mismatch.
Your Environment
- AWS Java SDK version used: 2
- JDK version used: 11
- Operating System and version: macOS 10.15.7
Hi @RushinNaik I'm able to reproduce it.
The optimistic locking works as expected for TransactWriteItemsEnhancedRequest putItem and updateItem, so this looks like a bug in deleteItem.
Thanks @debora-ito for looking into it. Is there any workaround for this? or any timeline for the fix.
As a workaround, you can use the DeleteItemEnhancedRequest conditionExpression
to define that the recordVersion value must be the same of the value fetched in step 2.
DeleteItemEnhancedRequest deleteItemRequest =
DeleteItemEnhancedRequest.builder()
.key(KEY)
.conditionExpression(Expression.builder()
.expression("recordVersion = :old_version_value")
.putExpressionValue(":old_version_value", oldVersionAttributeValue)
.build()).build();
If the condition is not met, the delete operation fails.
This workaround solved the issue for me. Thanks. Also, I would recommend putting a note in the existing documentation for this scenario.
Seems like if anything, this would be a bug in putItem not incrementing the java object's version once the item is successfully updated in the DB. That said, this could be expected behavior and not a bug. I.e. your code needs to make sure to increment the version upon a successful put/update.
Can we correct the documentation for deleteItem meanwhile before fixing this issue? https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html
Hi is anyone working on this issue?