views:

45

answers:

1

Consider this scenario; suppose I have WPF window which have four objects bonded to its controls (Schedule, Customer, Contract, ScheduleDetails and Signer specifically ) which represent four database tables in the backend database. I want when the user request save for the information he/she entered to be joined in atom operation in another words all saving operation be in one transaction so either all operations success or all failed. My question is what is the most efficient way to represent transaction operation in this scenario

+3  A: 

The most efficient is to use BeginTransaction etc on your DbConnection, but this isn't convenient, as you must use the same connection, and each DbCommand needs the transaction setting etc.

The simplest is TransactionScope, and if you are on SQL-Server 2005 or above, you will rarely notice a significant performance difference between this and BeginTransaction:

using(var tran = new TransactionScope()) {
    SaveA();
    SaveB();
    SaveC();
    SaveD();
    tran.Complete();
}

Here it doesn't matter if SaveA etc use the same connection, as SqlConnection will enlist into a TransactionScope automatically.

Alternatively, let an ORM handle this for you; most will create transactions for saving a group of changes.

One thing to watch; TransactionScope relies on services (DTC) that may not be available on all clients (since you mention WPF, which is client-side).

Marc Gravell
If you use TransactionScope make sure your target machine has the DTC - Distributed Transaction Coordinator service running otherwise you'll get exceptions.
Chris Marisic
@Chris - I was just editing for exactly that ;-p
Marc Gravell
@Mark, Thanks. But there's a relation between DTC and Transaction class -I don't remember when it comes to picture- but I want to avoid the overhead of being using DTC, even though i like the simple form of Transaction class. Is there a way not to be enlisted in DTC transaction?
Abdullah BaMusa
Oops, I comment befor you edit. any way the question is still there.
Abdullah BaMusa