views:

1083

answers:

2

I'm using System.Transactions and Transaction scope for my transaction handling in conjunction with the Enterprise Library Data Access Application Block.

In some cases I'm using separate instances of the Database class within the transaction. The connection is to the same database though.

I'm wondering if the application block will reuse the same connection and not span a distributed transaction in this case?

A: 

TransactionScope is a very strange beast. To my experience a DB transaction is promoted to a distributed one as soon as another connection (even to the same DB) gets open.

Anton Gogolev
+3  A: 

In a nutshell MSDN has your answer:

"Enterprise Library, on the other hand, normally opens and closes a connection for each request. This approach is incompatible with the way the TransactionScope class works. If there are multiple connections, the TransactionScope class considers the transaction to be a distributed transaction. Distributed transactions have a significant performance and resource overhead compared with a local transaction." (MSDN)

And

"To avoid this, the Database class methods, such as ExecuteDataSet, recognize when a TransactionScope instance is active and they enlist database calls in this transaction. If a transaction is currently active as a result of using a TransactionScope instance, the Database class methods use a single connection." (MSDN)

You don't mention what database you are using. In the Oracle 10g client I was using, it was the case that if you used TransactionScope you would always have a distributed transaction. It looks like this issue is now addressed in later releases.

You can check the answer by looking under Transaction Statistics.

RichardOD
Im using SQL Server 2005. I had read the above and thought that this meant in my above scenario the same connection would be used but it dosn't seem clear to me.
AJM
Try checking the stats to make sure that it isn't distributed.
RichardOD
You can get to this via-> Administrative tools -> Component services -> Computers -> My Computer -> Distributed Transaction Coordinator
RichardOD
Great, it seems to use the same connection. Thanks for the help
AJM