I am trying to use a transaction scope inside a loop. The entire loop takes place using a single connection to the database. I am using entity framework 4 for database access. During the second iteration of the loop, when the LINQ to Entites query executes, an exception is thrown stating that the MSDTC on the server is unavailable.
I've read that explicitly opening the connection and then enlisting the transaction is supposed to solve this problem, but it has not. Below is sample code that mirrors the basic operation that is taking place.
Any ideas on how to prevent an escalation to MSDTC?
Using context = New MyEntities()
Dim connection = context.Connection
connection.Open()
For index = 0 to (Me.files.Count - 1)
Dim query = From d In context.Documents
Where (d.DocumentID = documentID)
Select d.Status
Dim status = query.FirstOrDefault()
Using trans = New TransactionScope()
connection.EnlistTransaction(Transaction.Current)
Dim result = context.UpdateStatus(True)
If (result = 1) Then
WriteToFile()
trans.Complete()
End If
End Using
Next
End Using
Edit: Instead of TransactionScope, if I use connection.BeginTransaction(), transaction.Commit(), and transaction.Rollback(), it works fine. However, I would still like to find a way to make TransactionScope work.
The Microsoft Distributed Transaction Coordinator (MSDTC) service is a component of modern versions of Microsoft Windows that is responsible for coordinating transactions that span multiple resource managers, such as databases, message queues, and file systems.
TransactionScope is a class of System Namespace. It can also be termed as Transactions Namespace. The TransactionScope class supports transactions from code blocks and that is why it plays a key role in the . NET development framework. This class usually manages local as well as distributed transactions from our code.
TransactionScope
originally had an issue where it would promote a transaction to a distributed transaction when it met another connection, even if all connections were to the same database. This was a known issue in the framework.
I believe they addressed this in .NET 4, what version are you using?
A workaround has been provided in this answer:
Why is TransactionScope using a distributed transaction when I am only using LinqToSql and Ado.Net
Basically the same as the comment to your question suggesting to actually only use one physical connection from the pool - so only one connection gets enlisted.
Reviewing your question again I can see the above won't likely make a different, as you only use one connection anyway. Perhaps try closing and re-opening the connection on each iteration explicitly, and use the benefits of connection pooling.
Or more ideally, drop the use of TransactionScope
as IDbTransaction
has enough scope here to cover your code.
Did you check description for EnlistTransaction
in MSDN? It says:
New in ADO.NET 2.0 is support for using the EnlistTransaction method to enlist in a distributed transaction. Because it enlists a connection in a Transaction instance, EnlistTransaction takes advantage of functionality available in the System.Transactions namespace for managing distributed transactions. Once a connection is explicitly enlisted in a transaction, it cannot be unenlisted or enlisted in another transaction until the first transaction finishes.
It only mentions distributed transaction. You can try to use connection.BeginTransaction
instead. It will return instance of EntityTransaction
and you will call Commit
to complete transaction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With