Introduction
The responsibility of the TransactionScope class is to manage local as well as distributed transactions of our .NET Framework code block. The System.Transactions.TransactionScope namespace provides an implicit programming model by which transactions are automatically managed. It provides a simple mechanism for developers to specify a code block to be added in a transaction. TransactionScope is simple, straightforward, reliable, and easy to use. TransactionScope reduces the complexity of the code that needs to use transactions. Without using TransactionScope, a developer has to manage the transaction himself by writing additional code.
In this article, I will explain TransactionScope with various options, and demonstrate a sample application using TransactionScope.
Types of Transactions
A transaction is a unit of work that a developer wants to treat as “a whole.” It has to either happen in full or not at all. There are two types of transactions, as you’ll see in the following sections.
Business Transaction
A Business Transaction is a logical unit of work consisting of one or more activities involved between the Customer, Sales Person, Stakeholder, Vendor, and so forth. These transactions need to succeed or fail all activities to maintain the integrity of business information.
Database Transaction
A Database Transaction is a set of actions involved to manipulate the database(s). A databases transaction also ensures that you are not allowing something to access the data that another person is updating. It represents a state change. Transactions ideally have four properties, known by the acronym ACID:
- Atomic: The change cannot be divided or split in smaller parts.
- Consistent: The change can happen only if the new state of the system will be valid; any attempt to commit an invalid change will fail, leaving the system in its previous valid state.
- Isolated: No-one else sees any part of the transaction until it’s committed.
- Durable: If the system says the transaction has been committed, the client doesn’t need to worry about updating the system to make the change work.
Ways of Implementing Transaction Scope
Transaction Scope could be implemented as either Non-distributed or Distributed. Transaction scopes are dependent upon the following Data Access technologies:
- ADO .Net
- Entity Framework
- LINQ to SQL
Local Transaction Vs Distributed Transaction
A Non-distributed Transaction, or Local Transaction, works only with a single database and performs all data manipulation action on the same database.
On the other hand, a Distributed Transaction works with multiple databases. To apply Distributed Transaction, s developer should start the Distributed Transaction Coordinator service from the Services panel.
ADO.NET allows us to control the Local Transaction using given a Data Access methodology such as SQL Client, OLEDB, ODBC, and Oracle Client.
A traditional local transaction uses the BeginTransaction () and Commit () methods for implementing database transactions. The following example demonstrates that.
static void LocalNonTransactionScope() { string myconnectionString = ConfigurationManager .ConnectionStrings["MyTestDatabase"].ToString(); SqlTransaction mytransaction = null; using (var myconn = new SqlConnection(myconnectionString)) { myconn.Open(); try { mytransaction = myconn.BeginTransaction(); var mycommand = new SqlCommand("Insert Into Student (StudentID,StudentName) Values (@StudentID,@StudentName)", myconn); mycommand.Parameters.Add(new SqlParameter() { ParameterName = "@StudentID", DbType = System.Data.DbType.Int32, Value = 1111111 }); mycommand.Parameters.Add(new SqlParameter() { ParameterName = "@StudentName", DbType = System.Data.DbType.String, Value = "Sample Student" }); mycommand.ExecuteNonQuery(); mytransaction.Commit(); } catch (Exception) { if (mytransaction != null) { mytransaction.Rollback(); } } } }
The next code snippet shows the previous local transaction implemented by using the TransactionScope class.
static void LocalTransactionScope () { string myconnectionString = ConfigurationManager .ConnectionStrings["MyTestDatabase"].ToString(); using (TransactionScope myscope = new TransactionScope()) { using (var myconn = new SqlConnection(connectionString)) { myconn.Open(); var mycommand = new SqlCommand("Insert Into Student (StudentID,StudentName) Values (@StudentID,@StudentName)", myconn); mycommand.Parameters.Add(new SqlParameter() { ParameterName = "@StudentID", DbType = System.Data.DbType.Int32, Value = 1111111 }); mycommand.Parameters.Add(new SqlParameter() { ParameterName = "@StudentName", DbType = System.Data.DbType.String, Value = "Sample Student" }); mycommand.ExecuteNonQuery(); } myscope.Complete(); } }
This example shows how the .NET framework provides the TransactionScope class to maintain multiple transactions happening on multiple databases.
static void DistributedTransactionScope() { string myconnectionString = ConfigurationManager .ConnectionStrings["MyTestDatabase"].ToString(); string myconnectionString1 = ConfigurationManager .ConnectionStrings["MyTestDatabase2"].ToString(); using (TransactionScope myscope = new TransactionScope()) { using (var myconn = new SqlConnection(myconnectionString)) { myconn.Open(); var mycommand = new SqlCommand("Insert Into Student (StudentID,StudentName) Values (@StudentID,@StudentName)", myconn); mycommand.Parameters.Add(new SqlParameter() { ParameterName = "@StudentID", DbType = System.Data.DbType.Int32, Value = 1111111 }); mycommand.Parameters.Add(new SqlParameter() { ParameterName = "@StudentName", DbType = System.Data.DbType.String, Value = "Sample Student" }); mycommand.ExecuteNonQuery(); using (var myconn1 = new SqlConnection(myconnectionString1)) { conn1.Open(); var mycommand1 = new SqlCommand("Insert Into Subject (SubjectID,SubjectName) Values (@SubjectID,@SubjectName)", myconn1); mycommand1.Parameters.Add(new SqlParameter() { ParameterName = "@SubjectID", DbType = System.Data.DbType.Int32, Value = 44400001 }); mycommand1.Parameters.Add(new SqlParameter() { ParameterName = "@SubjectName", DbType = System.Data.DbType.String, Value = "Sample Subject" }); mycommand1.ExecuteNonQuery(); } } myscope.Complete(); } }
Conclusion
I hope the preceding explanation and code snippets will help developers understand how TransactionScope works. Enjoy coding and thanks for reading my post.