Transaction transaction in detail with so many transactions and their advantage
The advantages TransactionScope
are as follows:
- you don't need to commit the transaction (ADO.NET should auto-write)
- which means you can even use
TransactionScope
to add transactions to existing, closed source code (i.e. no changes required).
- which means you can even use
- a
TransactionScope
can (via DTC) span multiple resources (i.e. multiple databases or a database and MSMQ server, etc.).
But you pay for it a little at a rate. The join based operation is slightly faster (not much), but can only cover one resource and must be manually bound to your entire DAL code. However, if you are talking to only one instance of SQL2005 / SQL2008 then it can use "LTM" - which means it should not include DTC (which is where most of the performance comes from) unless absolutely necessary. Therefore, many operations TransactionScope
can only end the conversation with the database.
If you want to span 20 operations, then it TransactionScope
should be perfect - it saves you the trouble of committing transactions and allows each operation to manage its connection locally - making for very reusable code. Indeed, it TransactionScope
can be nested in the way you expect, so you can:
void Deposit(...) { /* uses TranScope to do a deposit */ }
void Debit(...) { /* uses TranScope to do a debit */ }
void Transfer(...) { /* uses a TranScope to span Debit/Deposit */ }
To do this with database transactions, you need to pass connection and transaction objects to each method, which quickly becomes clumsy - especially if you need to execute code to work with / without an existing open transaction (many " if(tran==null)
", etc.).
For more information see Transactions in .net
a source to share