Database.BeginTransaction vs Transactions.TransactionScope [duplicate]

I found out the answer in Entity Framework 6’s documentation: With the introduction of EF6, Microsoft recommends to use new API methods: Database.BeginTransaction() and Database.UseTransaction(). Although System.Transactions.TransactionScope is still very well supported, it is no longer necessary for most users of EF6. While Database.BeginTransaction() is used only for database related operations transaction, System.Transactions.TransactionScope, in addition … Read more

Why is System.Transactions TransactionScope default Isolationlevel Serializable

The fact Serializable is the default comes from times when .NET wasn’t even released (before year 1999), from DTC (Distributed Transaction Coordinator) programming. DTC uses a native ISOLATIONLEVEL enumeration: ISOLATIONLEVEL_SERIALIZABLE Data read by a current transaction cannot be changed by another transaction until the current transaction finishes. No new data can be inserted that would … Read more

How does TransactionScope roll back transactions?

Essentially TransactionScope doesn’t track your Adapter’s, what it does is it tracks database connections. When you open a DB connection the connections will looks if there is an ambient transaction (Transaction Scope) and if so enlist with it. Caution if there are more the one connection to the same SQL server this will escalate to … Read more

SQL Server: Isolation level leaks across pooled connections

The connection pool calls sp_resetconnection before recycling a connection. Resetting the transaction isolation level is not in the list of things that sp_resetconnection does. That would explain why “serializable” leaks across pooled connections. I guess you could start each query by making sure it’s at the right isolation level: if not exists ( select * … Read more

Get TransactionScope to work with async / await

In .NET Framework 4.5.1, there is a set of new constructors for TransactionScope that take a TransactionScopeAsyncFlowOption parameter. According to the MSDN, it enables transaction flow across thread continuations. My understanding is that it is meant to allow you to write code like this: // transaction scope using (var scope = new TransactionScope(… , TransactionScopeAsyncFlowOption.Enabled)) … Read more

Under what circumstances is an SqlConnection automatically enlisted in an ambient TransactionScope Transaction?

I’ve done some tests since asking this question and found most if not all answers on my own, since no one else replied. Please let me know if I’ve missed anything. Q1: Is connection automatically enlisted in transaction? Yes, unless enlist=false is specified in the connection string. The connection pool finds a usable connection. A … Read more

TransactionScope automatically escalating to MSDTC on some machines?

SQL Server 2008 can use multiple SQLConnections in one TransactionScope without escalating, provided the connections are not open at the same time, which would result in multiple “physical” TCP connections and thus require escalation. I see some of your developers have SQL Server 2005 and others have SQL Server 2008. Are you sure you have … Read more