title: Rolling back inner transaction when outer transaction fails
body: <p>I am using ORMLite to access a h2 database in Java.
  To perform transactions i'm using the static method <a href="http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/misc/TransactionManager.html#callInTransaction%28com.j256.ormlite.support.ConnectionSource,%20java.util.concurrent.Callable%29" rel="nofollow">TransactionManager.callInTransaction</a>.</p>
  
  <p>In case of single independent transactions this works fine. However in case the transactions are nested within each other the inner transactions get committed even if the outer transaction fails.</p>
  
  <p>As in this piece of pseudo-code:</p>
  
  <pre><code>OuterDatabaseTransaction
  {
    Loop
    {
      InnerDatabaseTransaction
      {
        //Multiple update or create statements
        //One of the InnerDatabaseTransactions throws a random exception
      }
      //Alternatively the OuterDatabaseTransaction throws a random
      //exception but all commited InnerDatabaseTransactions should rollback still
    }
  }
  </code></pre>
  
  <p>So what I would expect is that if any of the inner transactions fails the outer transaction fails too. And if the outer transaction fails none of the inner transactions get committed.
  At the moment it seems that each of the inner transactions are committed individually and do not for example share the same <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/Savepoint.html?is-external=true" rel="nofollow">Savepoint</a> as the outer transaction.</p>
  
  <p><strong>UPDATE</strong> (Thanks dened)</p>
  
  <p>Looking at the trace reveals the following</p>
  
  <pre><code>[TRACE] JdbcDatabaseConnection connection is closed returned false
  [TRACE] JdbcDatabaseConnection connection autoCommit is true
  [TRACE] JdbcDatabaseConnection connection set autoCommit to false
  [DEBUG] TransactionManager had to set auto-commit to false
  [TRACE] JdbcDatabaseConnection save-point sp14: id=0 name=ORMLITE15 set with name ORMLITE15
  [DEBUG] TransactionManager started savePoint transaction ORMLITE15
  [TRACE] JdbcDatabaseConnection connection is closed returned false
  [TRACE] JdbcDatabaseConnection connection autoCommit is false
  [TRACE] JdbcDatabaseConnection save-point sp15: id=0 name=ORMLITE16 set with name ORMLITE16
  [DEBUG] TransactionManager started savePoint transaction ORMLITE16
  [TRACE] JdbcDatabaseConnection connection is closed returned false
  [TRACE] JdbcDatabaseConnection update statement is prepared and executed returning 1: &lt;...&gt;
  [DEBUG] BaseMappedStatement update data with statement &lt;...&gt; changed 1 rows
  [TRACE] BaseMappedStatement update arguments: &lt;...&gt;
  [TRACE] JdbcDatabaseConnection connection is committed for save-point ORMLITE16
  [DEBUG] TransactionManager committed savePoint transaction ORMLITE16
  -&gt; [ERROR] TransactionManager after commit exception, rolling back to save-point also threw exception
  [TRACE] JdbcDatabaseConnection connection set autoCommit to true
  [DEBUG] TransactionManager restored auto-commit to true
  [TRACE] JdbcDatabaseConnection connection is closed returned false
  </code></pre>
  
  <p>Stepping into the source reveals that during rollback of <em>OuterDatabaseTransaction</em> the exception is thrown in the h2 source in <em>org.h2.engine.Session.java</em> within the function below. The reason behind that however I don't understand yet.</p>
  
  <pre><code>private void checkCommitRollback() {
    if (commitOrRollbackDisabled &amp;&amp; locks.size() &gt; 0) {
      throw DbException.get(ErrorCode.COMMIT_ROLLBACK_NOT_ALLOWED);
    }
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Posted <a href="https://sourceforge.net/p/ormlite/bugs/176/" rel="nofollow">ORMLite Bug Report</a></p>