title: Nested TransactionScope and/or nested connections causing MSDTC escalation
body: <p>I am trying to avoid MSDTC escalation in my application. I am using LINQ with SQL Server Express 2008 R2, and later will be using the full version.</p>
  
  <p>I have written a database wrapper class which creates connections as necessary and disposes of them as quickly as possible. The connection string remains the same across all connections. </p>
  
  <p>Here is a very slimmed-down version of my class:</p>
  
  <pre><code>public class SqlServerDatabaseWrapper {
  
    public SqlServerDatabaseWrapper(string connectionString) {
      ConnectionString = connectionString; 
    }
  
    public string ConnectionString { get; private set; }
  
    private static IDbConnection GetOpenConnection() {
      var conn = new SqlConnection(ConnectionString);
      conn.Open();
      return conn;
    }
  
    // there is also a second method to return a value
    // there is PerformCommandAction for SqlCommand as well
    public void PerformDataContextAction&lt;TContext&gt;(Func&lt;IDbConnection, TContext&gt; creator, Action&lt;TContext&gt; action) where TContext : DataContext {
      PerformConnectionAction(conn =&gt; {
        using (var context = creator(conn))
          action(context);
      });
    }
  
    // there is also a second method to return a value
    public void PerformConnectionAction(Action&lt;IDbConnection&gt; action) {
      using (IDbConnection conn = GetOpenConnection(ConnectionString)) {
        action(conn);
      }
    }
  }
  </code></pre>
  
  <p>Used as follows:</p>
  
  <pre><code>var db = new SqlServerDatabaseWrapper(connectionString);
  db.PerformDataContextAction(
    conn =&gt; new SomeDataContext(conn), 
    context =&gt; { /* do something */ }
  );
  </code></pre>
  
  <p>If I put a lock around the content of the PerformConnectionAction method, so only one can run at a time, then everything works but there is a noticeable performance penalty. However, when I remove it, it escalates.</p>
  
  <p>The code that is using the wrapper is using a TransactionScope, and there could be nesting of TransactionScopes and/or calls to either PerformDataContextAction or PerformConnectionAction (which each create a new connection with the same connection string); in pseudo-code (as this might occur across different classes/methods):</p>
  
  <pre><code>var db = new SqlServerDatabaseWrapper(connectionString)
  using (TransactionScope tran = new TransactionScope()) {
    db.PerformDataContextAction( 
      /* ... */,
      context =&gt; {
        using (TransactionScope tran2 = new TransactionScope()) {
          db.PerformConnectionAction(conn =&gt; { /* some stuff */ });
          tran2.Complete();
        }
      }
    tran.Complete();
  }
  </code></pre>
  
  <p>Note also that there is use of the static Membership methods which could occur at various points.</p>
  
  <p>I should also add that the connection string is as follows:</p>
  
  <pre><code>Data Source=.\SQLEXPRESS;Initial Catalog=db1;User Id=test1;Password=test1;MultipleActiveResultSets=true;Enlist=false;
  </code></pre>
  
  <p>The question is, how do I refactor/rewrite my code so that my application can perform well, without MSDTC, and without introducing locks?</p>
  
  <p>Thanks</p>