aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

Optimistic Locking while using Enhanced DDB Client.

Open RushinNaik opened this issue 3 years ago • 5 comments

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

  1. Insert item using TransactWriteItemsEnhancedRequest - PutItem.
  2. Fetch the item using table.getItem(GetItemEnhancedRequest). (recordVersion would be 1)
  3. Update one field of item using TransactWriteItemsEnhancedRequest - UpdateItem. (recordVersion will updated to 2 in the DDB.)
  4. 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

RushinNaik avatar Mar 30 '21 22:03 RushinNaik

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.

debora-ito avatar Apr 02 '21 22:04 debora-ito

Thanks @debora-ito for looking into it. Is there any workaround for this? or any timeline for the fix.

RushinNaik avatar Apr 02 '21 22:04 RushinNaik

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.

debora-ito avatar Apr 03 '21 00:04 debora-ito

This workaround solved the issue for me. Thanks. Also, I would recommend putting a note in the existing documentation for this scenario.

RushinNaik avatar Apr 05 '21 22:04 RushinNaik

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.

The-Zona-Zoo avatar Jul 20 '22 19:07 The-Zona-Zoo

Can we correct the documentation for deleteItem meanwhile before fixing this issue? https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html

saikumarsuvanam avatar Dec 16 '22 13:12 saikumarsuvanam

Hi is anyone working on this issue?

TheMarvelFan avatar Oct 10 '23 07:10 TheMarvelFan