We must have came across a situation where we have to call methods of different classes under same transaction scope and rollback the operations if any error occurs during execution of any methods. For eg. If we have Debit and Credit class and DebitAmount method of Debit class executed successfully but an error occurred while calling CreditAmount method of Credit class, we should be able to rollback all operations done by DebitAmount method. In scenario like this we can use System.Transactions.TransactionScope to rollback all operations done by DebitAmount method.
All most all of us generally use SqlTransaction in our day to day coding. But in this article I am going to explain about System.Transaction namespace that allows us to call different methods of different classes under a TransactionScope and commit the tranaction only if all of them are successfully executed.
SqlTransaction doesn't suits us in the scenaior explained above. As different methods of different classes are using their own SqlConnection object and we don't have any control over them. For easy understanding of the above scenario, I am going to take example of Debit and Credit class that has DebitAmount and CreditAmount method with parameter as accountNo and amount. In my TransferAmount method, I have to call DebitAmount method of Debit class and then CreditAmount method of Credit class so that the transfer of fund happen successfully. So my normal code will look like:
For more, visit http://www.dotnetfunda.com/articles/article70.aspx
Tuesday, May 06, 2008
Posted by Sheo Narayan at 7:06 PM