Transactionscope
Transactionscope makes it easy to manage transactions in .Net even if they are only simple one database access or involve multiple resources such as sql servers or WCF services. However there are a few rules:
In order for a resource manager (in this case SQL Server) to take part inside a transaction it must first enlist in the transaction if one already exists, SQL server determines this when a connection is first opened. Opening a connection when there is a transaction associated with the current thread will result in SQL server placing that connection inside the existing transaction, if no other enlistment has happened then SQL server will create a local transaction
and thus manage the transaction. Only if multiple resources are involved will the Distributed Transaction Coordinator (DTC) be brought into the picture.
We initiate the Transaction as follows:
using (TransactionScope scope = new TransactionScope(opt))
where opt is one of:
Required Join the ambient transaction, or create a new one if one does not exist
RequiredNew start a new transaction inside its own scope
Suppress no transaction
see http://msdn.microsoft.com/en-us/library/ms172152.aspx for more
So unless we are accessing multiple transactionable resources in our scope the DTC should not be envoked. This is easily proved by adding a line of code like:
Guid guid = Transaction.Current.TransactionInformation.DistributedIdentifier
This will be null is we are using Suppress and all zeros if the DTC is not used. of course the transactions are not completed till scope.Complete() is called which will not be the case when
an exception is thrown and the changes to our resources are rolled back.
In order for a resource manager (in this case SQL Server) to take part inside a transaction it must first enlist in the transaction if one already exists, SQL server determines this when a connection is first opened. Opening a connection when there is a transaction associated with the current thread will result in SQL server placing that connection inside the existing transaction, if no other enlistment has happened then SQL server will create a local transaction
and thus manage the transaction. Only if multiple resources are involved will the Distributed Transaction Coordinator (DTC) be brought into the picture.
We initiate the Transaction as follows:
using (TransactionScope scope = new TransactionScope(opt))
where opt is one of:
Required Join the ambient transaction, or create a new one if one does not exist
RequiredNew start a new transaction inside its own scope
Suppress no transaction
see http://msdn.microsoft.com/en-us/library/ms172152.aspx for more
So unless we are accessing multiple transactionable resources in our scope the DTC should not be envoked. This is easily proved by adding a line of code like:
Guid guid = Transaction.Current.TransactionInformation.DistributedIdentifier
This will be null is we are using Suppress and all zeros if the DTC is not used. of course the transactions are not completed till scope.Complete() is called which will not be the case when
an exception is thrown and the changes to our resources are rolled back.
Labels: transactionscope DTC

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home