dcsimg

Handling Transactions in .NET Using TransactionScope

WEBINAR:
On-Demand

Application Security Testing: An Integral Part of DevOps


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.



About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.