title: Spring Transaction - can't release Transaction after error
body: <p>I'm using Spring-tx-4.0.9 in a multithreaded application and have a problem with "stalled" transaction after the thread is killed by OutOfMemory.</p>
  
  <p>Each thread ask <code>PlatformTransactionManager</code> (implementation <code>org.springframework.jdbc.datasource.DataSourceTransactionManager</code>) to get an transaction via <code>TransactionTemplate</code> Propagation is REQUIRED. This template has a timeout set. State of a transaction is managed by application and after all, work is done, then thread calls commit. This returns a transaction back to the <code>PlatformTransactionManager</code>, and almost every time releases connection to the DB (in DB, the session for this connection is mostly released)</p>
  
  <p>But if the thread is killed by OutOfMemory, then the underlying transaction is returned to internal transaction pool and another thread can acquire this "broken" transaction. This transaction has the timeout set from the first time and cannot be reset even if new thread call TM to get transaction with <code>new TransactionTemplate</code>. So after this timeout expires, every request on transaction throws <code>TransactionTimedOutException</code>. </p>
  
  <p>Only available methods on <code>PlatformTransactionManager</code> are <code>getTransaction</code>, <code>commit</code> and <code>rollback</code>.
  Method <code>getTransaction</code> can return new or previous transaction. From returned instance of <code>TransactionStatus</code>, I'm able to detect new or stalled transaction, but I'm unable to release it. </p>
  
  <p>Method <code>rollback</code> sets flag <code>isRollbackOnly</code>, but the transaction is still not released and <code>getTransaction</code> returns stalled. If I then call <code>commit</code> as recommended by JavaDoc, then nothing changed and the transaction is still hanged.</p>
  
  <p>If I use propagation <code>REQUIRES_NEW</code>, then "broken" transaction is suspended, which solve a problem with <code>TransactionTimedOutException</code>, but an old transaction is still hanged and holds the connection to the DB (unwanted state).</p>
  
  <p>Is there a way, how to proper release a broken transaction (rollback and release connection)?</p>
  
  <p><strong>EDIT- Current code for getting a TransactionalStatus</strong> </p>
  
  <pre><code>public TransactionStatus beginTransaction() {
      // transaction begin
      logger.debug("Create new transaction.");
      TransactionTemplate template = new TransactionTemplate(transactionManager);
      template.setTimeout(txConnectionTimeout);
  
      TransactionStatus txStatus = transactionManager.getTransaction(template);
      logger.debug(
          "Transaction created. TransactionStatus: isCompleted=" + txStatus.isCompleted() + ", isNewTransaction=" +
          txStatus.isNewTransaction());
  
      return txStatus;
    }
  </code></pre>