views:

381

answers:

2

Hi,

I was wondering how you would use the TransactionScope class in the correct way when you are dealing with multithreading?

We create a new scope in our main thread and then we spawn of a couple of worker threads and we want these to participate in the main scope, so that for example the rollback is called on each worker if the scope is never completed.

I read something about TransactionScope using the ThreadStaticAttribute internally which made the above impossible / very difficult - could someone verify either way? If we run out code in a syncrhonized fashion then the rollbacks work, i.e the inner transactions are able to participate in the main transaction, but not if we switch over to a threaded execution.

Thanks

+3  A: 

See MSDN:

You should also use the TransactionScope and DependentTransaction class for applications that require the use of the same transaction across multiple function calls or multiple thread calls.

So maybe look into DependentTransaction - in particular, there is a worker thread example, here.

Marc Gravell
I also found these two blog posts useful: http://www.pluralsight.com/community/blogs/jimjohn/archive/2005/04/14/7332.aspx and http://www.pluralsight.com/community/blogs/jimjohn/archive/2005/05/01/7923.aspx
romkyns
+2  A: 

This is correct: the TransactionScope class uses the Transaction.Current property that stores its value in the field, which is marked with the ThreadStatic attribute.

The ThreadStatic attribute makes sure that the field value gets thread affinity, i.e. it has unique value in each thread. It's the recommended approach to share date within a thread. It's also known as Thread Local Storage (TLS).

The TransactionScope class just defines a transaction context in the current thread. It doesn't mean, however, that your code must accomplish all the job in that thread. I could imagine a complex calculation algorithm that uses multiple threads.

Michael Damatov