UpdateDataSet and System.Transactions.TransactionScope

Topics: Data Access Application Block
Feb 23, 2007 at 8:44 AM
Hi,

This question concerns EntLib 3.0 and the Data Access Application Block (DAAB) that has support for System.Transactions.

What can I do to make the UpdateDataSet method use the transaction in a System.Transactions.TransactionScope?

There are several method signatures that include a DbTransaction or an UpdateBehavior, but I do not know which choice will use the right transaction (that is the one from the TransactionScope).

Thanks
Herbjörn
Feb 23, 2007 at 1:55 PM
From my understanding, the System.Transactions namespace would work fine using the DAAB.

Here is a snippet of the simplest implementation:

using (TransactionScope txScope = new TransactionScope())
{
// ADO.NET / DAAB code

txScope.Complete();
}

If txScope.Complete() never gets called then the transaction is rolled back.
Feb 23, 2007 at 3:01 PM
You have two options for dealing with transactions in EntLib 3.0:

1) The DbTransaction Option which has always been available
2) The new System.Transactions / TransactionScope

If you choose to use the DbTransaction Option, you need to use the DAAB method overloads that accept a DbTransaction.

If you want to use TransactionScope, don't use those methods :) When using TransactionScope, the DAAB will automatically detect that there is a current transaction occurring and enlist itself in the transaction.

Hence, Greif is correct, just set-up your code like:

using (TransactionScope scope = new TransactionScope())
{
	// Do Something...
 
	scope.Complete();
}

I wrote a tutorial that may help you:

TransactionScope and Enterprise Library 3.0 Data Access Application Block - System.Transactions

Regards,

Dave

____________________________

David Hayden
Microsoft MVP C#

Feb 23, 2007 at 3:25 PM
Hi and thanks for Your answers.

My question is specific for the UpdateDataSet method. I have to either supply a DbTransaction or choose an appropriate UpdateBehavior.

Which update behavior will make DAAB use the transaction from the TransactionScope?
I guess that UpdateBehavior.Standard and UpdateBehavior.Continue does not create any transactions, but what will happen if they are executed within a TransactionScope?
Will UpdateBehavior.Transactional check for a TransactionScope and use that if it exists and create its own transaction if it does not exist?

Thanks again
Herbjörn
Feb 23, 2007 at 4:26 PM
All of the UpdateBehaviors will use the transaction from the TransactionScope if one exists.

The DAAB does check for an existing TransactionScope if you set UpdateBehavior.Transactional. If there is one, it is used. If there isn't one, the DAAB creates a DbTransaction for you and issues the Update within it.

Regards,

Dave
Feb 23, 2007 at 7:05 PM
Thanks Dave!

That is what I wanted to know

Sincerely
Herbjörn