title: Hanging transaction when open two SQL connections in nested transaction scopes
body: <p>I'm trying to understand what is wrong in the code below:</p>
  
  <pre><code>using (var scope1 = new TransactionScope())
  {
      var connection1 = new SqlConnection("...");
      connection1.Open();
  
      using (var scope2 = new TransactionScope())
      {
  
          SqlConnection connection2 = new SqlConnection("...");
          connection2.Open();
  
          connection2.Close();
          scope2.Complete();
  
      }
  
      connection1.Close();
      scope1.Complete();
  }
  </code></pre>
  
  <p>An application is quite complex therefore exists nested blocks of the <code>TransactionScope</code>. In the code above, when a connection has opened the transaction is created in SQL Server so actually, two transactions are created, because the connection is opened two times.</p>
  
  <p>I checked this by calling the following SQL statement:</p>
  
  <pre><code>SELECT * FROM sys.sysprocesses WHERE open_tran = 1
  </code></pre>
  
  <p>When the ambient transaction in scope1 has finished there is still one transaction in the database. The transaction created by the connection2.Close() is still hanging. My question is why this transaction still exists and how to correct the code.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I guess using one SQL connection defined outside the <code>TransactionScope</code> can be an answer, but maybe exists something better?</p>