title: hibernate multiple threads prevent multiple save(), JTA necessary?
body: <p>I'm using a hibernate session per request model for my web application.  My jdbc transaction begins at the beginning of each web request and commited at the end.</p>
  
  <p>// Non-managed environment idiom</p>
  
  <pre><code>Session sess = factory.openSession();
  Transaction tx = null;
  try {
      tx = sess.beginTransaction();
  
      // do some work
      ...
  
      tx.commit();
  }
  catch (RuntimeException e) {
      if (tx != null) tx.rollback();
      throw e; // or display error message
  }
  finally {
      sess.close();
  }
  </code></pre>
  
  <p>I'm faced with the problem where I am testing for existence of an entity (A) based on several parameters and doing an insert only if it doesn't exist.</p>
  
  <pre><code>public synchronized myMethod(param1, param2) {
      MyEntityA entity = MyEntityADAO.findEntity(param1, param2)
      if (entity == null) {
          entity = .../create entity
          MyEntityADAO.save(entity);
      }
  }
  </code></pre>
  
  <p>the problem is that synchronization does not help because the call to MyEntityADAO.save() does not actually write to the database when the currently running thread exits the method and releases the lock, the write to the database occurs after the transaction is commited which is generally what I need for my application except for a few scenarios.  The code above causes multiple records saved with same parameters in a multithreaded environment.</p>
  
  <p>I've tried to execute the save code in its own new session and transaction:</p>
  
  <pre><code>public synchronized myMethod(param1, param2) {
      MyEntityA entity = MyEntityADAO.findEntity(param1, param2)
      if (entity == null) {
          entity = .../create entity
          Session session = HibernateUtil.createSession();
          MyEntityADAO.save(entity);
          Transaction t = session.beginTransaction();
  
     }
  }
  </code></pre>
  
  <p>the above causes problems with 2 open sessions loading the same collection with hibernate in some instances.</p>
  
  <p>Should I enclose every DAO call in its own transaction and use transaction propagation with JTA? Is there a way to avoid JTA? Is it alright to commit transaction associated with the main session after the call to MyEntityADAO.save() and call beginTransaction on the main session right after and have the transaction commited at the end of the request as it does now?</p>