TransactionScope in a .NET Application

I wrote a block of code for a database transaction in a SQL Server database. I am using the TransactionScope class to rollback all changes in case of runtime crashes. If I run the application on a system that is running SQL SERVER, I have no problem. If I run the application on a system and SQL SERVER is running on a different system, I get an error that is disabled by MSDTC.

If I enable MSDTC on a running DBServer system, my application works fine.

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

Thank P.Gopalakrishnan.

+2


source to share


2 answers


TransactionScope, conceptually simple, hides a lot of magic behind the scenes. It allows one transaction to be distributed across multiple machines. To do this, it needs sophisticated support from the database - and the overhead may or may not be high.

You may need a simpler and more limited piece of technology: DbTransaction. Your Db provider can implement a transaction with semantics like sql BEGIN TRANSACTION

and COMMIT TRANSACTION

: in short, a transaction that only spans one connection on one machine.

Then your code would look 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 is no connection between them. Commands executed on different connections are not rolled back when the transaction is aborted (just like any C # code will not be rolled back when the transaction is aborted).

This approach is lighter, finer grained, and works for a wider range of database vendors, and does not require MSDTC downloads. On the other hand, transactions are connection specific (if you have multiple connections, they do not share the transaction), they cannot be distributed and you have to manually enlist the commands in the transaction (at least for MS -SQL).

Note that if you fail to enlist the command in the transaction, MS-SQL will throw an exception, SQLite will implicitly log the command, however, and I'm not sure what the oracle is doing. Since all commands must be registered in transactions, this is just an unauthorized redundant API, but this is not very problematic.

+1


source


TransactionScope supports (hybrid) fast local transactions with the ability to advance them to more expensive distributed transactions when required. MSDTC is required for distributed transactions.

The reason for moving towards distributed transactions is to include a second database in the same TransactionScope.



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

0


source







All Articles