title: Hibernate transaction and 'for update'
body: <p>I'm trying to obtain the lock on some rows on my mysql database using the "for update" statement. To do that I create a Criteria in the middle of an Hibernate Transaction and set a lock on it.</p>
  
  <pre><code>crit = session.createCriteria(AppIosVersion.class)
                  .add(Restrictions.eq("version", version))
                  .setLockMode(LockMode.PESSIMISTIC_WRITE);
  </code></pre>
  
  <p>Using setLockMode the query is correctly sent to the database, this is what I see in mysql.log:</p>
  
  <pre><code>     133 Query  SET autocommit=0
            133 Query SET autocommit=1
            133 Query SET autocommit=0
            133 Query select this_.Version as Version1_5_0_, this_.EditTimeStamp as EditTime2_5_0_, this_.IsActive as IsActive3_5_0_ from AppAndroidVersion this_ where this_.Version='0.2' for update
  130703 16:46:03   133 Query rollback
            133 Query SET autocommit=1
  </code></pre>
  
  <p>The problem is that the <em>for update</em> statement doesn't allow to acquire the lock because hibernate beginTransaction() doesn't become <em>START TRANSACTION</em> in mysql.</p>
  
  <p>My hibernate configuration is the following (i'm not using Spring).</p>
  
  <pre><code> &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  
  
     &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/Versions?autoReconnect=true&lt;/property&gt;
      &lt;property name="connection.username"&gt;name&lt;/property&gt;
      &lt;property name="connection.password"&gt;password&lt;/property&gt;
  
      &lt;!-- Session properties --&gt;
      &lt;!-- &lt;property name="hibernate.transaction.factory_class"&gt;org.hibernate.transaction.JDBCTransactionFactory&lt;/property&gt; --&gt; 
      &lt;property name="hibernate.current_session_context_class"&gt;org.hibernate.context.internal.ThreadLocalSessionContext&lt;/property&gt;
  
      &lt;property name="hibernate.connection.autocommit"&gt;false&lt;/property&gt;
  
      &lt;!-- configuration pool via c3p0--&gt;
      &lt;property name="hibernate.connection.provider_class"&gt;org.hibernate.connection.C3P0ConnectionProvider&lt;/property&gt;
      &lt;property name="hibernate.c3p0.max_size"&gt;100&lt;/property&gt;
      &lt;property name="hibernate.c3p0.min_size"&gt;10&lt;/property&gt;
      &lt;property name="hibernate.c3p0.acquire_increment"&gt;2&lt;/property&gt;
      &lt;property name="hibernate.c3p0.idle_test_period"&gt;180&lt;/property&gt;
      &lt;property name="hibernate.c3p0.max_statements"&gt;0&lt;/property&gt;
      &lt;property name="hibernate.c3p0.timeout"&gt;600&lt;/property&gt;
      &lt;property name="hibernate.c3p0.debugUnreturnedConnectionStackTraces"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.c3p0.unreturnedConnectionTimeout"&gt;180&lt;/property&gt;
  
      &lt;!-- SQL dialect --&gt;
      &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt; 
  </code></pre>