title: How to prevent concurrency issue in UPDATE via iBatis
body: <p>Our Java EE web application performs database operations using iBatis (ORM). The database operation flow is as below <br> </p>
  
  <p><em><strong>Flow</em></strong> :   JSP --->Action--->ServiceIMpl--->DaoImpl---->Calling update query thru' IBatis </p>
  
  <p><em><strong>Note</em></strong>: The Action, Service &amp; DAO classes are instantiated using Spring 2.5's Dependency Injection technique. We use Struts2.</p>
  
  <p><em><strong>Problem</em></strong>: 2 concurrent users search for the same record and User1 updates Attribute1 while User2 updates Attribute2. User1 first clicks on 'Save' and then User2 clicks on save (they follow each other with a difference of few seconds). When we see the data in database, only the User1's update is present. The Audit columns also show only the User1's update. The User2's update on Attribute2 is not done , at the sametime, no Exceptions are thrown.</p>
  
  <p>We tried using the begin transaction and commit transaction in iBatis surrounding the invokation of the update query defined in the sqlmap xml. The same issue persists. We also tried removing those transaction commands, yet the problem persists.</p>
  
  <p>Should we do anything special/different to handle concurrency during updates ? Wouldn't ORMs like iBatis handle by themselves ?</p>
  
  <p><em><strong>Additional Info</em></strong>: Further investigation revealed the following information. <br></p>
  
  <p>When User1 &amp; User2 clicks on the a record, it's complete data is fetched for viewing. </p>
  
  <p>Now when both User1 &amp; User2 changes few attributes and click save (concurrently), say first User1's data is updated and then while User2's data is being updated, the already updated data of User1 is over-written &amp; lost.</p>
  
  <p><em><strong><code>What approach is usually followed in such screens to handle such scenarios ?</code></em></strong></p>