I am trying to implement both DbContext
(SQLDBContext
& DB2Context
) in single transaction but every time facing an issue related to DB2.
It works fine with SQL but throws error when trying to access DB2.
The exception is :
Error in DB2Entities getter.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)
Please help me to implement both the DB transactions under single Transaction OR if one of them fails then both should rollback.
Code is like:
var option = new TransactionOptions
{
IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted,
Timeout = TimeSpan.FromSeconds(60)
};
using (var scopeOuter = new TransactionScope(TransactionScopeOption.Required, option))
{
SQLDBContext.Table.AddSomething();
SQLDBContext.SaveChanges();
using (var scopeInner = new TransactionScope(TransactionScopeOption.Required, option))
{
DB2Context.Table.AddSomething();
DB2Context.SaveChanges();
scopeInner.Complete();
}
scopeOuter.Complete();
}
Thank you!
With DB2, you will have to enable XA Transactions for MSDTC. Since you are also using multiple databases, you may have to also enable Network DTC Access (see image below).
To change these settings open the Component Services management snap-in (Administrative Tools -> Component Services, or run comexp.msc
). Then under Computers -> My Computer -> Distributed Transaction Coordinator, right click on "Local DTC" and hit properties. You will get the screen below.
User contributions licensed under CC BY-SA 3.0