Dynamo DB Optimistic Locking Behavior during Save Action

亡梦爱人 提交于 2019-12-24 09:59:30

问题


Scenario: We have a Dynamo DB table supporting Optimistic Locking with Version Number. Two concurrent threads are trying to save two different entries with the same primary key value to that Table.

Question: Will ConditionalCheckFailedException be thrown for the latter save action?


回答1:


Yes, the second thread which tries to insert the same data would throw ConditionalCheckFailedException.

com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException

As soon as the item is saved in database, the subsequent updates should have the version matching with the value on DynamoDB table (i.e. server side value).

save — For a new item, the DynamoDBMapper assigns an initial version number 1. If you retrieve an item, update one or more of its properties and attempt to save the changes, the save operation succeeds only if the version number on the client-side and the server-side match. The DynamoDBMapper increments the version number automatically.




回答2:


We had a similar use case in past but in our case, multiple threads reading first from the dynamoDB and then trying to update the values.

So finally there will be change in version by the time they read and they try to update the document and if you don't read the latest value from the DynamoDB then intermediate update will be lost(which is known as update loss issue refer aws-docs for more info).

I am not sure, if you have this use-case or not but if you have simply 2 threads trying to update the value and then if one of them get different version while their request reached to DynamoDB then you will get ConditionalCheckFailedException exception.

More info about this error can be found here http://grepcode.com/file/repo1.maven.org/maven2/com.michelboudreau/alternator/0.10.0/com/amazonaws/services/dynamodb/model/ConditionalCheckFailedException.java



来源:https://stackoverflow.com/questions/43757608/dynamo-db-optimistic-locking-behavior-during-save-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!