Entity Framework and Distributed Transaction Coordinator

1

I'm at my wits end trying to figure out why my transaction scope is giving me the following error:

"An exception occurred while initializing the database. See the InnerException for details."

--> "The underlying provider failed on Open."

--> "Communication with the underlying transaction manager has failed."

 --> "The MSDTC transaction manager was unable to pull the transaction
       from the source transaction manager due to communication problems.
       Possible causes are: a firewall is present and it doesn't have an
       exception for the MSDTC process, the two machines cannot find each
       other by their NetBIOS names, or the support for network transactions
       is not enabled for one of the two transaction managers. (Exception
       from HRESULT: 0x8004D02B)"

As a good user of Stackoverflow, I have search for and applied many solutions but still this error is persistent. Below are some of the questions and answers I've checked out:

MVC 3 : The MSDTC transaction manager was unable to pull the transaction from the source

The MSDTC transaction manager was unable to pull the transaction from the source transaction manager due to communication problems

The transaction manager has disabled its support for remote/network transactions

So let me give you some background info of the system in working in. Basically it has uses a reversed engineered Entity Framework using a generic repository. However, there are two MS SQL databases used, which are on the same server. The result of this is that there are two db contexts. On top of that, lays a unit of work pattern, which is used in a service layer that is wrapping them in a transaction scope. So the code looks something like this:

using(var transactionManager = TrasactionManager)
{
    using (var unitOfWork = FirstUnitOfWork)
    {
        //Setup linq queries and get data to modify and or delete entities...

        unitOfWork.Commit(); //DBContext.SaveChanges();
    }

    using (var unitOfWork = SecondUnitOfWork)
    {
        //Setup linq queries. Fails here...

        unitOfWork.Commit(); //DBContext.SaveChanges();
    }

    transactionManager.Complete();
}

Now, the error is thrown when the first query in the second unit of work using block is called.

As you can see, when the unitOfWork.Commit() method is called it calls the SaveChanges() method in the db context. The using block then disposes of the unitOfWork which in turn calls the db context Dispose() method. Although both unit of works are setting up separate db contexts for the two databases, using separate connection strings, which get close when the using statement finishes. So I wonder if this is not the problem in that perhaps that the db contexts need to share a connection string. And if so, how do i go about doing that?

Perhaps someone has another idea about how to fix this error? I'm considering to separate out the two using statements into separate method calls but this will be problematic if a roll back is required.

I hope that my question and details are clear?

Thanks in advance

c#
entity-framework
transactions
transactionscope
msdtc
asked on Stack Overflow Oct 7, 2015 by MacGyver • edited May 23, 2017 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0