简体   繁体   中英

Transaction Scope giving underlying connection failed to open

For LINQ i am using TransactionScope to handle transactions. When i execute the queries i get an exception.

Underlying connection failed to Open.

and the inner exception is saying something about DTC. I read online that i will have to enable some service on the server. But i donot want to do that. How can i use transcactions without enabling DTC. My code is something like this

public void function1()
{
     using(TransactionScope t = new TransactionScope())
     {
         RunSomeSelectQueries();
         RunSomeInsertQueries();
         RunSomeUpdate Queries();

         t.Complete();
     }
}

Thanks in advance.

You have a serious problem in you tx handling that will push al ot of load into various levels.

  • TransactioNScope is nice, but
  • Calling methods that open separate connections WITHOUT NEED is bad.

Here is why:

  • If a Tx has one connection, it is handled in the kernel as local transaciton scope.
  • If you have multiple connections (transacted ressources) you need DTC.

And:

  • DTC is much slower than just a connection, with a lot more load on the server (multiple open connections - they have to stay open until the transaction commits). Also that turns into a multi step commit - generally a lot of overhead and making things slower.

That generally is a "hey, I just use transactions" antipattern.

Properly you should make sure you only create one database connection UNLESS YOU NEED MORE THAN ONE (like multiple databases are involved) so that the transaction scope does not proppagate to the DTC for having multiple ressources.

Obviously you also should configure DTC correctly IF you need it, but again: in this case the real problem is that you abuse the transaction scope forcing a DTC propagation where none is actually needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM