Question

I was wondering if it's a good practice to nest two transactions? For example wrapping my NHibernate transaction with TransactionScope for the benefit of the Tests (making sure that the db rollbacks all the changes that were made in the test).

The other option is to keep the entities that I insert into the Db in memory and delete them at the end of the test.

Which one is better?

Was it helpful?

Solution

First of all, nhibernate doesn't support nested transactions!

TransactionScope on the other side will not create a new transaction if there is already one opened. If you only use transaction scope, it will create a new transaction for the connection.

If you then open a transaction within the scope, this will still work with nhibernate.

Back to your question, it pretty much depends on the amount of objects you create within the TransactionScope. If it becomes too many, you will simply spam the transaction log of your database. Apart from that, the concept is perfectly fine I would say.

And one important thing to mention, if you use TransactionScope, and you create multiple sessions/transaction with nhibernate, the scope might switch to distributed transactions which requires MSDTC to run on the target server, otherwise it will simply fail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top