Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Gotchas when using TransactionScope and MS DTC

I am just starting to work with using TransactionScope, I find that there are always unexpected things I run into that take forever to debug.

I figure that having a consolidated list of these would be great for those "weird error" circumstances, plus to expand our knowledge of oddness in the platform.

Some context on how I am going to be using transaction scopes:

  • web application
  • multiple web servers, application servers and sql servers
  • transactions will be mainly database transactions but some will be elevated to write to MSMQ.
like image 364
Gord Avatar asked Nov 18 '09 21:11

Gord


People also ask

How to resolve MSDTC issue?

Reinstalling MSDTC may reset MSDTC Security Configuration options back to default values. Verify that the MSDTC Security Configuration options are set to the appropriate values after reinstalling MSDTC. Reinstalling MSDTC may change the Startup Type value for the Distributed Transaction Coordinator service.

What is TransactionScope?

The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically.

What is MSDTC used for?

Microsoft Distributed Transaction Coordinator (popularly known as MSDTC) is a coordinator to handle the distributed transactions. A distribution transaction involves two or more databases in a single transaction.

Is MSDTC necessary?

Is MSDTC required? MSDTC is not required for Sql Server installation or operation. If you are only going to use Database Engine, then it is not required or used. If your Sql uses any of the above mentioned query techniques (Linked Server, OPENQUERY, etc), or SSIS or Workstation Components then MSDTC is required.


2 Answers

2 things off the top of my head:

  • transactions will be elevated when you use more than one connection object in the same scope, even if the connections have the same connectionstring (this is fixed in sql 2008). Read more in this thread and dbconnectionscope will solve that problem on sql 2005
  • msdtc instances need to be able to see each other and need to have their security set up correctly http://support.microsoft.com/kb/899191 (allow inbound and outbound, do not require mutual authentication is usually the safest bet). Use DTCPing to troubleshoot connection issues between dtc instances as explained here: http://support.microsoft.com/kb/306843

You want transactions to be lightweight as much as possible, dtc introduces a lot of overhead. You also want transactions to be as short as possible, so introduce them only on the application servers and not on the web server. Make the hop over the network between the application servers and the database as small as possible and as fast as possible, send network traffic between web and app servers over a different connection than between app servers and db, and make the last one a screaming fast, ridiculously short connection.

If you have multiple application servers, you could consider having a single instance of msdtc running on a server (e.g. on the database or on one of the app servers) and use this remotely from all application servers instead of each one running their own, but I don't know what additional benefits this has.

like image 88
stombeur Avatar answered Sep 30 '22 16:09

stombeur


Hopefully this will help someone one day:

If you have a TransactionScope with multiple SQL operations inside, the DTC will not be involved provided

  1. you use identical connection string for each connection
  2. connections are not nested.

I.e. open, do someting, close. open, do something, close.

Now for the gotcha: if you ever do this in your process (on another thread)

SqlConnection.ClearAllPools()

and that happens to come between your two operations - the DTC will be involved immediately. If you don't have the DTC running, it will throw an Exception.

like image 24
Dodgyrabbit Avatar answered Sep 30 '22 18:09

Dodgyrabbit