Transaction Scope — A simple way to handle transactions in c#

Mahrukh Mehmood
3 min readFeb 9, 2019

--

Are you familiar with transactions? Have you ever used transactions in c#?
We often need transactions when we need to do CRUD operations on multiple tables in same or even different data sources.

Make sure you know your scope when you start!

What are Transactions?

A transaction can be considered as a set of operations or can called as atomic logical unit that can work with multiple data sources. If a transaction fails means any of the operation doesn’t completed for any reason, then all the operations will be rolled back.

Transaction Scope

Transaction Scope is introduced in .Net 2.0 in System.Transaction namespace. It helps us to handle transactions with an easy way by giving us free authority to write our database code and logical code in separate functions or classes and call them directly in transactions. You don’t need to add multiple nested if else conditions and make your code extra long as by doing that you will loose your code readability and efficiency.

How to use Transaction Scope?

To use transaction scope, you need to use add System.Transaction namespace in your class.

By Using Single Data Source

try
{
using (TransactionScope scope = new TransactionScope())
{
// Your first operation with SQL Connection String
// Your second operation
// ...
// if success so far, commit the transaction
scope.Complete();
}
}
catch (Exception ex)
{
// transaction will be rolled back if exception occurs
// ...
}

scope.Complete() indicates that all operations within the scope are finished without any failure.

By Using Multiple Data Source

try
{
using (TransactionScope scope = new TransactionScope())
{
// Your first operation with SQL Connection String A
using (connection = new SqlConnection(connectionStringA))
{
connection.Open();
// Your first operation
// Your second operation
// ...
}
// Your second operation with SQL Connection String B
using
(connection = new SqlConnection(connectionStringB))
{
connection.Open();
// Your first operation
// Your second operation
// ...
}
// ... // if success so far, commit the transaction
scope.Complete();
}
}
catch (Exception ex)
{
// transaction will be rolled back if exception occurs
// ...
}

When all the operations are performed successfully, you should inform the Transaction Manager to commit the transaction by calling Complete method. If you do not call this method, your transaction will be aborted.

Using Disposable Block

I have used Disposable block while instantiating TransactionScope class, as it makes sure the dispose gets called when it gets out of the block and ends the transaction scope.

Please remember that if you are not using Disposing Block, you need to call Dispose method after calling Complete method of Transaction Scope.

I have personally found it very easy in one of my business process of user registration. From registration to verification of users, from addition of member to deactivation of members.. I have used this amazing library to keep my code simple and manageable and recommend my colleagues to use this whenever needed.

--

--

Mahrukh Mehmood

Technical Lead | Programmer | Technical Writer | Web Architect