Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nice work Triynko, your answers all look quite accurate and complete to me. Some other things I would like to point out:</p> <p><strong>(1)</strong> Manual enlistment</p> <p>In your code above, you (correctly) show manual enlistment like this:</p> <pre><code>using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (TransactionScope ts = new TransactionScope()) { conn.EnlistTransaction(Transaction.Current); } } </code></pre> <p>However, it is also possible to do it like this, using Enlist=false in the connection string.</p> <pre><code>string connStr = "...; Enlist = false"; using (TransactionScope ts = new TransactionScope()) { using (SqlConnection conn1 = new SqlConnection(connStr)) { conn1.Open(); conn1.EnlistTransaction(Transaction.Current); } using (SqlConnection conn2 = new SqlConnection(connStr)) { conn2.Open(); conn2.EnlistTransaction(Transaction.Current); } } </code></pre> <p>There is another thing to note here. When conn2 is opened, the connection pool code doesn't know that you want to later enlist it in the same transaction as conn1, which means that conn2 is given a different internal connection than conn1. Then when conn2 is enlisted, there are now 2 connections enlisted so the transaction must be promoted to MSDTC. This promotion can only be avoided by using automatic enlistment.</p> <p><strong>(2)</strong> Before .Net 4.0, I highly recommend setting <a href="https://stackoverflow.com/questions/195420/transactionscope-bug-in-net-more-information/3527164">"Transaction Binding=Explicit Unbind" in the connection string</a>. This issue is fixed in .Net 4.0, making Explicit Unbind totally unnecessary.</p> <p><strong>(3)</strong> Rolling your own <code>CommittableTransaction</code> and setting <code>Transaction.Current</code> to that is essentially the same thing as what <code>TransactionScope</code> does. This is rarely actually useful, just FYI.</p> <p><strong>(4)</strong> <code>Transaction.Current</code> is thread-static. This means that <code>Transaction.Current</code> is only set on the thread that created the <code>TransactionScope</code>. So multiple threads executing the same <code>TransactionScope</code> (possibly using <code>Task</code>) is not possible. </p>
    singulars
    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. 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.
    3. VO
      singulars
      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