title: Problems with TransactionScope and Oracle
body: <p>we have written a C# 3.5 client talking to an Oracle database (11g) using the ODP.NET.</p>
  
  <p>This application has a batch process where a long running task is performed making various calls to the database within a TransactionScope.</p>
  
  <p>On our development environment all goes well, but at the UAT environment of one of our clients (who has loads of data) two alternating (sometimes the one, sometimes the other...) errors occur:</p>
  
  <ol>
  <li>Unable to enlist in a distributed transaction</li>
  <li>The transaction has aborted. (inner exception: Transaction Timeout)</li>
  </ol>
  
  <p>We currently use a time-out of <strong>one day</strong> for the transaction (for testing purposes).</p>
  
  <p>Running said process on the UAT environment causes to halt after approx. 10 mins with one of above exceptions, so no way near the timeout value.</p>
  
  <p>Here's a snippet of the stacktrace for the second error:</p>
  
  <pre><code>at System.Transactions.TransactionStatePromotedAborted.CreateAbortingClone(InternalTransaction tx)
     at System.Transactions.DependentTransaction..ctor(IsolationLevel isoLevel, InternalTransaction internalTransaction, Boolean blocking)
     at System.Transactions.Transaction.DependentClone(DependentCloneOption cloneOption)
     at System.Transactions.TransactionScope.SetCurrent(Transaction newCurrent)
     at System.Transactions.TransactionScope.PushScope()
     at System.Transactions.TransactionScope..ctor(TransactionScopeOption scopeOption)
     at System.Transactions.TransactionScope..ctor()
     at Application.Domain.DataAccess.Oracle.EntityDaoBase`2.SaveItem(TEntity item, EntityReference`1 user)
  </code></pre>
  
  <p>The process tries to save an item to the DB within the transaction scope, but the stacktrace shows that the constructor is hit for the TransactionScope class, meaning it creates a new TransactionScope.</p>
  
  <p>Am I right so far?</p>
  
  <p>Because I don't know much of the inner workings of the TransactionScope, but it seems like when you call a method within the scope, it will create a new transaction (assumingly inheriting from the ambient transaction).</p>
  
  <p>Could it be that if I am right, that this new transaction does not inherit the correct timeout (but the default one), so that a nested transaction will cause this timeout exception?</p>
  
  <p>If not, any thoughts on what it possibly can be? On a side note, there are no nested transactions defined within the methods called from within the ambient transaction.</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>Simplified code snippet of the function:</p>
  
  <pre><code>public void SomeLengthyBatchProcess()
  {
     using (var transaction = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(1, 0, 0, 0)))
     {
         foreach (var item in Items)
         {
            SaveItemToDB(item);
         }
  
         transaction.Complete();
     }
  }
  
  public void SaveItemToDB(object item)
  {
     using (var transaction = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(1, 0, 0, 0)))
     {
         // Performing data persistency here
  
         transaction.Complete();
     }
  }
  </code></pre>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Okay, so as it turns out, there <strong>is</strong> a nested transaction going on in the method 'SaveItemToDB'. After some digging through the code a colleague made, I saw that it has its own TransactionScope defined, but without options and timeout.</p>
  
  <p>After modifying this method so that it has the same parameters regarding timeout, I ran the code again on the customer's server and still no luck (again the transaction aborted error with the time out).</p>
  
  <p>So my questions are now as follows:</p>
  
  <ol>
  <li>Is it necessary to define timeout values for nested transactions or do they inherit this from the ambient transaction?</li>
  <li>How is it possible that a timeout exception can occur when the timeout setting is (presumably, aside from inner workings that I do not know about) the same for all transaction scopes and has a timeout value defined of 1 day, where the exception occurs after approx. 10 minutes?</li>
  <li>Is it possible to prevent Oracle from creating a distributed transaction for transactions where the connectionstring is the same?</li>
  <li>Can it be that the added overhead of a distributed transaction causes exceptions like the  transaction aborted one?</li>
  </ol>
  
  <p>I updated the code snippet so it better reflects the situation.</p>
  
  <p><em>(btw: the second, nested transaction, is necessary because the DAL also seperately persists some child items, if present, and the whole item should of course, be rolled back if anything goes wrong while persisting the child items)</em></p>
  
  <p>Hopefully with this addition it will be easier to shed some light on this issue!</p>