Search code examples
amazon-dynamodb

Does DynamoDB.transactionWrite change the value of version for java object that is being updated?


I am using dynamoDB transactionWrite (with dynamiDBMapper 1.11.x) to write to 2 different dynamoDB tables in sync. I have following code:-


Entity entity = EntityConverter.toEntity(domainModel)
HistoryEntity history = HistoryEntity.toHistory(domainModel)

TransactionWriteRequest transactionWriteRequest = new 
transactionWriteRequest.addUpdate(entity)
transactionWriteRequest.addUpdate(historyEntity)
dynamoDBMapper.transactionWrite(transactionWriteRequest);

Entity class(which is a DDB document) has a version attribute that is annotated with @DynamoDBVersionAttribute annotation and I am relying on ddb's version management. When I check the version of entity object in above code just before and after the ddb transaction, I see that version if entity object is being incremented by ddb. Is this expected? I know that DDB assigns value to attributes that are annotated with DyanmoDbVersionAttribute annotation (from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html ), but is it expected that ddb changes the version value for the java entity object also?

I tried logging the version before and after the transactionWrite and observed that it is getting changed. I also tried to read from guide https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html but couldn't find anything in the official guide.


Solution

  • Once DynamoDB has persisted the data successfully, it will update the local version of the image to match the new version, which in turn will increment the VersionedAttribute.