Common Gotchas when using TransactionScope and MS DTC

I am just starting to work with TransactionScope, I find that there are always unforeseen things that I encounter in order to debug forever.

I believe that having a consolidated list of them would be good for these "strange mistakes", as well as for expanding our knowledge of the oddness in the platform.

Some context of how I will use transaction areas:

  • web application
  • multiple web servers, application servers and sql servers
  • transactions will be mostly database transactions, but some will be raised for writing to MSMQ.
+6
transactionscope msdtc
source share
4 answers

2 things from the head:

  • transactions will be increased if you use more than one connection object in the same scope, even if connections have the same connection string (this was fixed in sql 2008). More in this topic and dbconnectionscope will solve this problem in sql 2005
  • msdtc instances should be able to see each other and must correctly configure their security http://support.microsoft.com/kb/899191 (allow incoming and outgoing, does not require mutual authentication, usually the safest rate). Use DTCP to troubleshoot connectivity issues between dtc instances as described here: http://support.microsoft.com/kb/306843

You want transactions to be as easy as possible, dtc introduces a lot of overhead. You also want transactions to be as short as possible, so check them out only on application servers, not on a web server. Make network transfer between application servers and the database as small and fast as possible, send network traffic between web application and application servers to a different connection than between application servers and db, and make the last quick cry, ridiculously short connection.

If you have several application servers, you can consider using one instance of msdtc on the server (for example, in a database or on one of the application servers) and use it remotely from all application servers, and not from each of them, but I don’t know what additional benefits it has.

+3
source share

Also note that if you change any objects during a transaction, they will not be rolled back unless you add code to handle this.

See TransactionScope and Object State Scan

+1
source share

Hope this helps someone someday:

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

  • you use the same connection string for each connection.
  • connections are not nested.

those. open, do something, close. open, do something, close.

Now for getting: if you ever do this in your process (in another thread)

SqlConnection.ClearAllPools()

and this happens between your two operations - DTC will be activated immediately. If you do not have a DTC, it will throw an exception.

0
source share

If you use SQL Server and check @@ trancount, it will be 0, even if you have an active TransactionScope.

-2
source share

All Articles