I'm trying to understand if DynamoDB Optimistic Locking is the correct thing for my use case or should I be doing something else.
I'm trying to do the following in my Java method.
function updateItem(String key) {
Item item = mapper.load(Item.class, key);
if (some condition) {
item.setValue(item.getValue() + 1);
mapper.save(item);
}
}
I want to update the same item based on some condition succeeding. I've created a version attribute, so that Optimistic locking works and when I have multiple requests coming in, only one request gets and updates the data.
I'm trying to understand the following:
What happens when some other thread tries to update the value but the version id has changed, I couldn't find any documentation on what exception will be thrown?
Should I be using a synchronized function for this? Considering multiple requests will be coming in? But, to me this seems like it defeats the purpose of optimistic locking, since I don't care which request gets access first.
Is there an alternate solution to this problem?
I've been through the following documentation: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html