Before the TransactionScope class can be understood, some background knowledge is required.
A transaction is a set of steps that make up one work unit. This means that for a transaction, either all of its steps need to be successfully completed or none of them. Therefore, it is considered a single work unit.
Transactions have a set of properties. These properties are Atomicity, Consistency, Isolation, and Durability. Together, they make up the acronym, ACID.
TransactionScope is a class in the System.Transaction
namespace. A code-block can be made transactional using this class. The code within the implementation will be considered one transaction.
The following code shows how this class can be used.
using (var transactionScope = new System.Transactions.TransactionScope()){try{//Write code here which is needed to be transactionaltransactionScope.Complete(); //Call this function once transaction is complete}catch(Exception ex){//If an exception is thrown, Complete() is not called, and the transaction is rolled back}}
In case an exception is raised within the scope before the complete function is called, the transaction will be rolled back.
Free Resources