views:

277

answers:

3

I want to run set of different commands on different tables in parallel and in a transactional way, how can I do that?

More details:

I want all the commands to be distributed over threads but in a single transaction, so if all the threads succeed I'll commit else rollback.

+1  A: 

A typical database connection is not capable of handling multithreaded usage, so you shouldn't attempt that.

Instead you might look into DTC, which allows for multiple database connections to cooperate in a single transaction. It carries some overhead though, so you should see if you really get the multithreaded benefits you want.

Lasse V. Karlsen
This means there is no any way from .Net to do it??
Ahmed Said
I did not say that, what I mean is that you need something more than just a database connection object and multithreaded code.
Lasse V. Karlsen
Ahmed, this is more about what MSQL server supports than .NET - it would be the same with any language using the ODBC driver.
weismat
+4  A: 

Use the TransactionScope-class with DependentTransactions. See http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx and http://msdn.microsoft.com/en-us/library/system.transactions.dependenttransaction.aspx.

This will use DTC most likely, so make sure that you are up to speed on that as well.

Jonas Lincoln
A: 

I want to run set of different commands on different tables in parallel and in a transactional way, how can I do that?

From thread #1: open a connection, start a transaction, issue commands, commit the transaction.

From thread #2: open a connection, start a transaction, issue commands, commit the transaction

RickNZ
I want all the commands to be distributed over threads but in single transaction, all or nothing
Ahmed Said