views:

106

answers:

3

I've just begon using Transactions in .NET and I have a problem. In a function (in my DAL) I use a transaction scope. On the end of the function I trigger the Complete() function.

Now I have a test for this function which also uses a transaction scope. On the end of this test function I do not trigger the complete() function so that my test doesn't fill the database with test data. This seems to be working fine.

My problem now is that if I do trigger complete() in my test function, the transaction seems to be completed twice. This seems logical as I trigger Complete() twice... but is there a way to avoid this? Is there a way to trigger the Complete() function twice without executing the scope twice... Sure there must be a way to nest transactions...?

A: 

My understanding is that if you Dispose without calling Complete the transaction will be rolled, also TransactionScope supports nested transactions. Mix and match and you should be able to roll back all the work your tests do.

Sam Saffron
A: 

Try creating your inner TransactionScope with the RequiresNew option:

new TransactionScope(TransactionScopeOption.RequiresNew)
Philippe Leybaert
Thx, I will first look into these nested transactions and the TransactionScopeOption. I tried the RequiresNew which solves my previous problem but this raises new problems.
Lieven Cardoen
+1  A: 

You need to understand nested transaction scopes and the TransactionScopeOption.

Pavel Nikolov