title: How does Hibernate control the transaction with multiple session.update()s if you don't commit the transaction explicitly?
body: <p>I have inherited a system that uses Spring MVC and Hibernate that stores into a DB2 database. Recently it started having what looks like a deadlock problem reported by DB2 so I've been working on debugging it. I'm not a Spring or Hibernate expert but I know some of both.</p>
  
  <p>The code is structured like this:</p>
  
  <pre><code>controller -&gt; service -&gt; one or more DAO classes that performs the updates
  </code></pre>
  
  <p>In the situation where the problem occurs the controller calls a service method annotated with <code>@Transactional</code> and <code>@Override</code>. The service goes to a DAO class annotated with <code>@Repository</code> and calls multiple methods annotated with <code>@Transactional</code> and <code>@Override</code>. That is the structure of code. </p>
  
  <p>One controller calls one service method. The service method calls multiple DAO methods. Most of the DAO methods are marked <code>@Transactional</code> and <code>@Override</code>.  A few have REQUIRES_NEW in the <code>@Transactional</code>.  Also in each DAO method the <code>Session</code> is gotten by <code>SessionFactory.getCurrentSession()</code>.</p>
  
  <p>First question: with this approach where does the transaction start and where is the ultimate commit? And the rollback? None of this is explicitly coded.</p>
  
  <p>The deadlock comes when one of the methods updates a record and then a subsequent method deletes that same record. If you are wondering why anyone wants to do this the update adds the user id of the person wanting to delete the information in the record so that it can be saved by a stored process to another table. Then the second method call deletes the record.</p>
  
  <p>The original coder tried adding REQUIRES_NEW to the <code>@Transactional</code> thinking a new transaction would ensure the update would happen. But the deadlock still happens at irregular intervals.</p>
  
  <p>I tried changing the update to a native SQL call but Hibernate won't let me start another transaction since one is already open.</p>
  
  <p>Anyone have any guidance here?</p>