title: JPA and optimistic locking modes
body: <p>I read an article on the blog of Oracle <a href="https://blogs.oracle.com/enterprisetechtips/entry/locking_and_concurrency_in_java" rel="noreferrer">here</a> about JPA and locking modes.</p>
  
  <p>I don't entirely understand the difference between <code>OPTIMISTIC</code> and <code>OPTIMISTIC_FORCE_INCREMENT</code> lock mode types.</p>
  
  <p><strong><em><code>OPTIMISTIC</code> mode :</em></strong></p>
  
  <p><img src="https://i.stack.imgur.com/ccPcD.jpg" alt="enter image description here"></p>
  
  <p>When a user locks an entity with this mode, a check is done on the version field entity (<code>@version</code>) at the beginning of transaction and a check on the version field is also done at the end of the transaction. If versions are different, the transaction rolls back. </p>
  
  <p><strong><em><code>OPTIMISTIC_FORCE_INCREMENT</code> mode :</em></strong></p>
  
  <p><img src="https://i.stack.imgur.com/IjpC6.jpg" alt="enter image description here"></p>
  
  <p>When a user chooses this mode, he has to flush() the state of EntityManager into database to increment the version field manually. Thus, all other optimistic transactions will be invalidated (roll back). A check on the version is also done at end of transaction to commit or roll back transaction.</p>
  
  <p>It seems clear but when should I use <code>OPTIMISTIC</code> versus <code>OPTIMISTIC_FORCE_INCREMENT</code> modes ? The only criteria that I see is to apply <code>OPTIMISTIC_FORCE_INCREMENT</code> mode when I want the transaction to have precedence over the others because choosing this mode will roll back all other running transactions (if I understand well mecanism).</p>
  
  <p>Is there other reason to choose this mode rather than <code>OPTIMISTIC</code> mode?</p>
  
  <p>Thanks</p>