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.
- public void UpdateProductsAndCatagories(List < Categories > ProductCategoies, List < Products > LanguageSpecificXmlDocuments)
- {
- var isupdatedProducts = false;
- var isUpdatedCategories = false;
- var updateProductWatch = new Stopwatch();
- updateProductWatch.Start();
- try
- {
-
- var ProductCategoriesFromSource = GetProductCategoriesfromSourceDB();
-
- var SourceProducts = GetProductsfromSourceDB();
-
- var ProductCategoriesFromDest = GetProductCategoriesfromDestDB();
-
- var DestinationProducts = GetProductsfromDestDB();
- var option = new TransactionOptions
- {
- IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TimeSpan.FromSeconds(Convert.ToInt32(5000))
- };
-
- using(var scopeOuter = new TransactionScope(TransactionScopeOption.Required, option))
- {
-
-
-
-
-
-
- isUpdatedCategories = UpdateBasicProductCategories(ProductCategoriesFromSource, ProductCategoriesFromDest);
-
-
-
- if (isUpdatedCategories && isupdatedProducts)
- {
-
- scopeOuter.Complete();
- var SucessMsg = string.Format("Successfully Updated products and categories.");
- Console.WriteLine(SucessMsg);
- updateProductWatch.Stop();
- }
-
- else
- {
-
- if (!isupdatedProducts)
- {
-
- scopeOuter.Dispose();
- var errormsg = string.Format("Failed updating products ,rolls back the all current transaction's for products.");
- Console.WriteLine(errormsg);
- updateProductWatch.Stop();
- }
-
- if (!isUpdatedCategories)
- {
-
- scopeOuter.Dispose();
- var errormsg = string.Format("Failed updating product categories,rolls back the all current transaction's for product categories.");
- updateProductWatch.Stop();
- }
- }
- }
- }
- catch (SqlException ex)
- {
- var errormsg = string.Format("Failed updating,issue is : " + ex.Message);
- updateProductWatch.Stop();
- }
-
- catch (Exception ex)
- {
- var errormsg = string.Format("Failed updating,issue is : " + ex.Message);
- updateProductWatch.Stop();
- }
- }