Transactionscope And Method Call That Uses The Same Connection
Solution 1:
If more then one connection are open under same TransactionScope it will be automatically escalated to the DTC.
You need to close first connection before calling Method2.
publicstaticvoidMethod1()
{
using (TransactionScope scope = new TransactionScope())
{
bool success = true; // will be set to false in an omitted catchbool isSomethingHappened
using (var connection = new SqlConnection(ConnectionString1))
{
isSomethingHappened = // Execute query 1
}
if(somethingHappened)
Method2();
if(success)
scope.Complete();
}
}
Solution 2:
Nested connections under the same transaction scope will promote to a distributed transaction.
From SQL server 2008 and above multiple (not nesting) connections under the same transaction scope will not promote to a distributed transaciton.
see this question for more information
Solution 3:
I don't know the precise answer, but I would make the connection a member, and keep track if it's open.
Then in Method1 and Method2 I would get the connection via some GetConnection() which would open the connection on first use.
After reading the comments I would suggest a private DoMethod2 which takes in a connection object.
Post a Comment for "Transactionscope And Method Call That Uses The Same Connection"