title: TransactionScope automatically escalating to MSDTC on some machines?
body: <p>In our project we're using TransactionScope's to ensure our data access layer performs it's actions in a transaction. We're aiming to <em>not</em> require the MSDTC service to be enabled on our end-user's machines. </p>
  
  <p>Trouble is, on half of our developers machines, we can run with MSDTC disabled.  The other half must have it enabled or they get the <em>"MSDTC on [SERVER] is unavailable"</em> error message. </p>
  
  <p>It's really got me scratching my head and has me seriously considering rolling back to a home-spun TransactionScope-like solution based on ADO.NET transaction objects. It's seemingly insane - the same code that works (and does not escalate) on half of our developer's <strong>does</strong> escalate on the other developer's.</p>
  
  <p>I was hoping for a better answer to <a href="https://stackoverflow.com/questions/506733/">Trace why a transaction is escalated to DTC</a> but unfortunately it doesn't. </p>
  
  <p>Here's a sample bit of code that will cause the trouble, on the machines that try to escalate, it tries to escalate on the second connection.Open() (and yes, there is no other connection open at the time.)</p>
  
  <pre><code>using (TransactionScope transactionScope = new TransactionScope() {
     using (SqlConnection connection = new SqlConnection(_ConStr)) {
        using (SqlCommand command = connection.CreateCommand()) {
           // prep the command
           connection.Open();
           using (SqlDataReader reader = command.ExecuteReader()) {
              // use the reader
              connection.Close();
           }
        }
     }
  
     // Do other stuff here that may or may not involve enlisting 
     // in the ambient transaction
  
     using (SqlConnection connection = new SqlConnection(_ConStr)) {
        using (SqlCommand command = connection.CreateCommand()) {
           // prep the command
           connection.Open();  // Throws "MSDTC on [SERVER] is unavailable" on some...
  
           // gets here on only half of the developer machines.
        }
        connection.Close();
     }
  
     transactionScope.Complete();
  }
  </code></pre>
  
  <p>We've really dug in and tried to figure this out.  Here's some info on the machines that it works on:</p>
  
  <ul>
  <li>Dev 1: Windows 7 x64 SQL2008</li>
  <li>Dev 2: Windows 7 x86 SQL2008 </li>
  <li>Dev 3: Windows 7 x64 <strike>SQL2005</strike> <strong>SQL2008</strong></li>
  </ul>
  
  <p>Developers it doesn't work on:</p>
  
  <ul>
  <li>Dev 4: Windows 7 x64, <strike>SQL2008</strike> <strong>SQL2005</strong></li>
  <li>Dev 5: Windows Vista x86, SQL2005</li>
  <li>Dev 6: Windows XP X86, SQL2005</li>
  <li>My Home PC : Windows Vista Home Premium, x86, SQL2005</li>
  </ul>
  
  <p>I should add that all machines, in an effort to hunt down the problem, have been fully patched with everything that's available from Microsoft Update.</p>
  
  <h1>Update 1:</h1>
  
  <ul>
  <li><a href="http://social.msdn.microsoft.com/forums/en-US/windowstransactionsprogramming/thread/a5462509-8d6d-4828-aefa-a197456081d3/" rel="noreferrer">http://social.msdn.microsoft.com/forums/en-US/windowstransactionsprogramming/thread/a5462509-8d6d-4828-aefa-a197456081d3/</a> describes a similar problem...back in 2006!</li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope%28VS.80%29.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope%28VS.80%29.aspx</a> - read that code sample, it clearly demonstrates a nested-second connection (to a second SQL server, actually) which will escalate to DTC.  <em>We are not doing this in our code</em> - we're not using different SQL servers, nor different connection strings, nor do we have nested secondary connections opening - <em>there should not be escalation to DTC</em>.</li>
  <li><a href="http://davidhayden.com/blog/dave/archive/2005/12/09/2615.aspx" rel="noreferrer">http://davidhayden.com/blog/dave/archive/2005/12/09/2615.aspx</a> (from 2005) talks about how escalation to DTC will always happen when connecting to SQL2000.  We're using SQL2005/2008</li>
  <li><a href="http://msdn.microsoft.com/en-us/library/ms229978.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms229978.aspx</a> MSDN on transaction escalation. </li>
  </ul>
  
  <p>That MSDN transaction-escalation page states that the following conditions will cause a transaction to escalate to DTC:</p>
  
  <ol>
  <li>At least one durable resource that does not support single-phase notifications is enlisted in the transaction.</li>
  <li>At least two durable resources that support single-phase notifications are enlisted in the transaction. For example, enlisting a single connection with does not cause a transaction to be promoted. However, whenever you open a second connection to a database causing the database to enlist, the System.Transactions infrastructure detects that it is the second durable resource in the transaction, and escalates it to an MSDTC transaction.</li>
  <li>A request to "marshal" the transaction to a different application domain or different process is invoked. For example, the serialization of the transaction object across an application domain boundary. The transaction object is marshaled-by-value, meaning that any attempt to pass it across an application domain boundary (even in the same process) results in serialization of the transaction object. You can pass the transaction objects by making a call on a remote method that takes a Transaction as a parameter or you can try to access a remote transactional-serviced component. This serializes the transaction object and results in an escalation, as when a transaction is serialized across an application domain. It is being distributed and the local transaction manager is no longer adequate.</li>
  </ol>
  
  <p>We're not experiencing #3. #2 is not happening because there is only ever one connection at a time, and it's also to a single 'durable resource'.  Is there any way that #1 could be happening?  Some SQL2005/8 configuration that causes it to not support single-phase notifications?</p>
  
  <h1>Update 2:</h1>
  
  <p>Re-investigated, personally, everyone's SQL Server versions - "Dev 3" actually has SQL2008, and "Dev 4" is actually SQL2005.  That'll teach me to never trust my coworkers again. ;)  Because of this change in data, I'm pretty sure we've found our problem.  Our SQL2008 developers weren't experiencing the problem because SQL2008 has copious amounts of awesome included that SQL2005 does not have.</p>
  
  <p>It also tells me that because we're going to be supporting SQL2005 that we can't use TransactionScope like we have been, and if we want to use TransactionScope we're going to need to be passing a single SqlConnection object around...which seems problematic in situations where the SqlConnection can't easily be passed around...it just smells of global-SqlConnection instance.  Pew!</p>
  
  <h1>Update 3</h1>
  
  <p>Just to clarify up here in the question:</p>
  
  <p>SQL2008:</p>
  
  <ul>
  <li>Allows multiple connections within a single TransactionScope (as demonstrated in the above sample code.)</li>
  <li>Caveat #1: If those multiple SqlConnections are nested, that is, two or more SqlConnections are opened at the same time, TransactionScope will immediately escalate to DTC.  </li>
  <li>Caveat #2: If an additional SqlConnection is opened to a different <em>'durable resource'</em> (ie: a different SQL Server,) it will immediately escalate to DTC</li>
  </ul>
  
  <p>SQL2005:</p>
  
  <ul>
  <li>Does not allow multiple connections within a single TransactionScope, period.  It will escalate when/if a second SqlConnection is opened.</li>
  </ul>
  
  <h1>Update 4</h1>
  
  <p>In the interest of making this question even more <strike>of a mess</strike> useful, and just for more clarity's sake, here's how you can get SQL2005 to escalate to DTC with a <em>single</em> <code>SqlConnection</code>:</p>
  
  <pre><code>using (TransactionScope transactionScope = new TransactionScope()) {
     using (SqlConnection connection = new SqlConnection(connectionString)) {
        connection.Open();
        connection.Close();
        connection.Open(); // escalates to DTC
     }
  }
  </code></pre>
  
  <p>This just seems broken to me, but I guess I can understand if every call to <code>SqlConnection.Open()</code> is grabbing from the connection pool.  </p>
  
  <p><em>"Why might this happen, though?"</em>  Well, if you use a SqlTableAdapter against that connection before it's opened, the SqlTableAdapter will open and close the connection, effectively finishing the transaction for you because you now can't re-open it.</p>
  
  <p>So, basically, in order to successfully use TransactionScope with SQL2005 you need to have some sort of global connection object that remains open from the point of the first TransactionScope is instantiated until it's no longer needed.  Besides the code-smell of a global connection object, opening the connection first and closing it last is at odds against the logic of opening a connection as late as possible and closing it as soon as possible.  </p>