views:

391

answers:

2

Hi All,

I have written a code block to do database transaction in SQL Server database. I am using TransactionScope class to rollback all the changes if any failures during the execution. If I run the application in a system which running SQL SERVER, I have no problem. If I run the application in a system and the SQL SERVER is running in another system, I am receiving an error that is MSDTC is disabled.

If I enable the MSDTC in DBServer running system, my application is working fine.

If I have to user ORACLE db, how the system will work? Should I configure something on the ORACLE DB Server to enable transaction scope? Please help me.

Thanks, P.Gopalakrishnan.

+1  A: 

TransactionScope, while conceptually simple, hides a lot of magic behind the scenes. It enables you to use one transaction that's distributed over several machines. To do so, it needs complex support from the database - and the overhead may or may not be high.

A simpler more limited piece of technology, may suffice for you: DbTransaction. Your Db provider can implement a transaction with semantics similar to sql's BEGIN TRANSACTION and COMMIT TRANSACTION: in short, a transaction that spans only a single connection on a single machine.

Your code would then look something like...

DbCommand cmd = ...;
using (DbTransaction trans = Connection.BeginTransaction()) {
  cmd.Transaction = trans; //sometimes optional, though MS-SQL requires it.

  cmd.ExecuteNonQuery()
  [...other db commands with the same connection and transaction...]
  trans.Commit();
}

To be crystal clear: you can use several other connections while the transaction is open - but there's no link between them. Commands executed on different connections aren't rolled back on transaction abort (much like arbitrary C# code won't be rolled back on transaction abort).

This approach is lighter weight, more finely grained, and works for a broader range of database providers, and it doesn't require the MSDTC to boot. On the flip side, transactions are connection-specific (if you have several connections, they don't share the transaction), they can't be distributed, and you'll need to manually enlist commands into the transaction (at least for MS-SQL).

Note, if you fail to enlist a command in a transaction, MS-SQL will throw an exception, SQLite will implicitly enroll the command nevertheless, and I'm not sure what oracle does. Since all commands must be enrolled in the transactions anyhow, this is just an unhandily redundant API, but it's not terribly problematic.

Eamon Nerbonne
A: 

TransactionScope supports (hybrid) fast local transactions with the option to promote them to more expensive distributed transactions, when ever required. For distributed transactions, MSDTC is required.

A reason for promoting to distributed transactions is if you involve a second database in the same TransactionScope.

Oracle Data Provider version 10.2.0.3 and higher do support local transactions + distributed transactions (same as for SQL Server 2005 and higher). Older versions of ODP.NET only support distributed transactions.

taoufik