A Transaction Scope Or Nested Transaction

A transaction scope can select and manage the ambient transaction automatically. Due to its ease of use and efficiency, it is recommended that you use the Transaction Scope class when developing a transaction application.

Completing a transaction scope

When your application completes all the work it wants to perform in a transaction, you should call the Complete method only once to inform the transaction manager that it is acceptable to commit the transaction.

TransactionAbortedException

A TransactionAbortedException is thrown if the scope creates the transaction, and the transaction is aborted. A TransactionIndoubtException is thrown if the transaction manager cannot reach a Commit decision. No exception is thrown if the transaction is committed.

Rolling back a transaction

If you want to rollback a transaction, you should not call the Complete method within the transaction scope. For example, you can throw an exception within the scope. The transaction in which it participates will be rolled back.

Example:

As a system admin if you want to update product and categories detail as a single unit, use Transaction Scope class to complete this task.

With the below example you can compare source database products and categories, compare all data and update all data to destination data. If insert, update or delete fails for any single process transaction scope aborts all data, if transaction is successful for both process then only it proceeds for commit.

  1. public void UpdateProductsAndCatagories(List < Categories > ProductCategoies, List < Products > LanguageSpecificXmlDocuments)  
  2. {  
  3.     var isupdatedProducts = false;  
  4.     var isUpdatedCategories = false;  
  5.     var updateProductWatch = new Stopwatch();  
  6.     updateProductWatch.Start();  
  7.     try  
  8.     {  
  9.         // Retrieve All categories from source Database   
  10.         var ProductCategoriesFromSource = GetProductCategoriesfromSourceDB();  
  11.         //Get all products from source database  
  12.         var SourceProducts = GetProductsfromSourceDB();  
  13.         // Retrieve All categories from destination Database   
  14.         var ProductCategoriesFromDest = GetProductCategoriesfromDestDB();  
  15.         //Get all products from destination database  
  16.         var DestinationProducts = GetProductsfromDestDB();  
  17.         var option = new TransactionOptions  
  18.         {  
  19.             IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TimeSpan.FromSeconds(Convert.ToInt32(5000))  
  20.         };  
  21.         //here we handle nested transaction with TransactionScope logic  
  22.         using(var scopeOuter = new TransactionScope(TransactionScopeOption.Required, option))  
  23.         {  
  24.             //1. Insert,update,Delete products logic   
  25.             //first compare data from source to destination and then apply updation logic.  
  26.             //perform insert,update,delete using sql stored procedure or sql statemen isupdatedProducts = UpdateProducts(SourceProducts, DestinationProducts);  
  27.             //2. Insert,update,Delete product categories logic   
  28.             //first compare data from source to destination and then apply updation logic.  
  29.             //perform insert,update,delete using sql stored procedure or sql statement  
  30.             isUpdatedCategories = UpdateBasicProductCategories(ProductCategoriesFromSource, ProductCategoriesFromDest);  
  31.             // The Complete method commits the transaction. If an exception has been thrown,   
  32.             // Complete is not called and the transaction is rolled back.  
  33.             //if all 2 updater process successful then go for commit else rollback all transactions.  
  34.             if (isUpdatedCategories && isupdatedProducts)  
  35.             {  
  36.                 //commit transaction   
  37.                 scopeOuter.Complete();  
  38.                 var SucessMsg = string.Format("Successfully Updated products and categories.");  
  39.                 Console.WriteLine(SucessMsg);  
  40.                 updateProductWatch.Stop();  
  41.             }  
  42.             //transaction is rolled back.  
  43.             else  
  44.             {  
  45.                 //if failed updating product data  
  46.                 if (!isupdatedProducts)  
  47.                 {  
  48.                     //rollback transaction   
  49.                     scopeOuter.Dispose();  
  50.                     var errormsg = string.Format("Failed updating products ,rolls back the all current transaction's for products.");  
  51.                     Console.WriteLine(errormsg);  
  52.                     updateProductWatch.Stop();  
  53.                 }  
  54.                 //if failed updating product Categories stop proceeding and rollback all current transaction  
  55.                 if (!isUpdatedCategories)  
  56.                 {  
  57.                     //rollback transaction   
  58.                     scopeOuter.Dispose();  
  59.                     var errormsg = string.Format("Failed updating product categories,rolls back the all current transaction's for product categories.");  
  60.                     updateProductWatch.Stop();  
  61.                 }  
  62.             }  
  63.         }  
  64.     }  
  65.     catch (SqlException ex)  
  66.     {  
  67.         var errormsg = string.Format("Failed updating,issue is : " + ex.Message);  
  68.         updateProductWatch.Stop();  
  69.     }  
  70.     //General exception handling   
  71.     catch (Exception ex)  
  72.     {  
  73.         var errormsg = string.Format("Failed updating,issue is : " + ex.Message);  
  74.         updateProductWatch.Stop();  
  75.     }  
  76. }  

 


Recommended Free Ebook
Similar Articles