Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have just tried how escalation to distributed transaction behaves in SQL Server 2012 (EXPRESS). And I have observed the same behaviour as for SQL Server 2008.</p> <p>I have tested it on very simple code with <code>SqlConnection</code> objects. There are always two database access (INSERTs) in one transaction scope.</p> <p><strong>Not nested connections - NOT ESCALATING</strong></p> <p><em>This is the only option if you want to avoid the promotion to DTC. Access only one durable resource (database) during the transaction and closing one connection before opening another. (Eventually you can avoid DT by reusing the same connection but it is not necessary when you use Connection pooling and would probably make your code less readable.)</em></p> <pre><code>static string connectionString = "data source=.\\;Integrated Security=SSPI;Database=TestDB"; static void Main(string[] args) { using (var scope = new TransactionScope()) { using (var conn = new SqlConnection(connectionString)) { conn.Open(); using (var command = conn.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = "INSERT INTO TestTable(Value) VALUES (@1)"; command.Parameters.Add(new SqlParameter("1", "123456")); command.ExecuteNonQuery(); } } using (var conn2 = new SqlConnection(connectionString)) { conn2.Open(); using (var command2 = conn2.CreateCommand()) { command2.CommandType = CommandType.Text; command2.CommandText = "INSERT INTO TestTable(Value) VALUES (@1)"; command2.Parameters.Add(new SqlParameter("1", "123456")); command2.ExecuteNonQuery(); } } scope.Complete(); } } </code></pre> <p><strong>Nested connections - ESCALATING</strong></p> <pre><code>static string connectionString = "data source=.\\;Integrated Security=SSPI;Database=TestDB"; static void Main(string[] args) { using (var scope = new TransactionScope()) { using (var conn = new SqlConnection(connectionString)) { conn.Open(); // EXECUTE INSERT using (var conn2 = new SqlConnection(connectionString)) { conn2.Open(); // EXECUTE INSERT } } scope.Complete(); } } </code></pre> <p><strong>Not nested connections accessing different databases - ESCALATING</strong></p> <p><em>If you access two or more durable resources during the transaction it will be always promoted to DTC.</em> </p> <pre><code>static string connectionString = "data source=.\\;Integrated Security=SSPI;Database=TestDB"; static string connectionString2 = "data source=.\\;Integrated Security=SSPI;Database=TestDB2"; static void Main(string[] args) { using (var scope = new TransactionScope()) { using (var conn = new SqlConnection(connectionString)) { conn.Open(); // EXECUTE INSERT } using (var conn2 = new SqlConnection(connectionString2)) { conn2.Open(); // EXECUTE INSERT } scope.Complete(); } } </code></pre> <p><strong>Note</strong> It does not matter what kind of CRUD operation you will execute, it always depends in which manner the connections are opened and closed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload