Tim Schmelter Tim Schmelter - 12 days ago 6
C# Question

TransactionScope and method call that uses the same connection

I'm using

to make a method that contains multiple sql statements transactional. Now i need to call a second method that also uses the same connection and i receive following exception at
connection.Open()
:


Network access for Distributed Transaction Manager (MSDTC) has been
disabled. Please enable DTC for network access in the security
configuration for MSDTC using the Component Services Administrative
tool.


So this is the pseudo-code:

public static void Method1()
{
using (TransactionScope scope = new TransactionScope())
{
bool success = true; // will be set to false in an omitted catch
using (var connection = new SqlConnection(ConnectionString1))
{
// ...
if(somethingHappened)
Method2();
}
if(success)
scope.Complete();
}
}

public static void Method2()
{
using (var connection = new SqlConnection(ConnectionString1))
{
connection.Open(); // BOOOM!
// ...
}
}


How to avoid this exception without repeating the code from
Method2
in
Method1
?

Answer

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.

public static void Method1()
{
    using (TransactionScope scope = new TransactionScope())
    {
        bool success = true; // will be set to false in an omitted catch

        bool isSomethingHappened
        using (var connection = new SqlConnection(ConnectionString1))
        {
           isSomethingHappened = // Execute query 1
        }

       if(somethingHappened)
           Method2();

        if(success)
            scope.Complete();
    }
}
Comments