title: Correct way to use DynamoDB Optimistic Locking
body: <p>I'm trying to understand if DynamoDB Optimistic Locking is the correct thing for my use case or should I be doing something else.</p>
  
  <p>I'm trying to do the following in my Java method.</p>
  
  <pre><code>function updateItem(String key) {
      Item item = mapper.load(Item.class, key);
      if (some condition) {
          item.setValue(item.getValue() + 1);
          mapper.save(item);
      }
   }
  </code></pre>
  
  <p>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.</p>
  
  <p>I'm trying to understand the following:</p>
  
  <ol>
  <li><p>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?</p></li>
  <li><p>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.</p></li>
  <li><p>Is there an alternate solution to this problem?</p></li>
  </ol>
  
  <p>I've been through the following documentation:
  <a href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html" rel="noreferrer">http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html</a></p>