Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET / ASP.NET4.0
Tip/Trick

.NET TransactionScope and its default Transaction Isolation level issue

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
1 Feb 2013CPOL1 min read 50.4K   9   7
.NET TransactionScope and its default Transaction Isolation level issue.

Introduction 

We often use the .NET Framework TransactionScope object to manage/handle database transactions. Often we create an instance of TransactionScope object like the following:

C#
var scope = new
TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 1, 0)) 

Or:

C#
var scope = new TransactionScope() 

or use some other overloaded constructor. Everything works fine. But the story begins when a timeout exception message comes.

Details 

Actually it is a deadlock exception. So anyone can think it happened for concurrent user access for same resource (database row) and it might be a very rare case and ignore it. But unfortunately it comes again and again and you cannot overlook it. I start rnd for understanding the reason why the deadlock happens again and again. Why is it happening? First I start investigating the TransactionScope object. I tried to find if there are any issues in that component. Finally I got my answer. The investigating result is the TransactionScope object's default Isolation Level is Serializable. That is the culprit for raising such a deadlock exception for some scenarios. If we need to fix that we should use other isolation levels like ReadCommitted, etc. As we know SQL Server uses ReadCommitted as the default Isolation Level. It is recommended to use that for general purposes.

How to Fix

Create a factory method in the business layer (considering transaction will be managed by the Business Layer).

The Factory method body is as follows:

public static TransactionScope CreateTransactionScope()
 { 
    var transactionOptions = new TransactionOptions 
    { 
        IsolationLevel = IsolationLevel.ReadCommitted,
        Timeout = net TimeSpan(0,0,0,0,10,0) //assume 10 min is the timeout time
    }; 
    return new TransactionScope(TransactionScopeOption.Required, transactionOptions); 
} 

Use of Factory method

Instead of creating a TransactionScope object with default constructor like the following

using (var scope = new 
TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 1, 0)))

we can create that object with the help of a factory method and it will fulfill our purpose.

C#
using (var scope =  CreateTransactionScope() )   { 
    saveProjectEntity();
    scope.Complete(); 
} 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Bangladesh Bangladesh
How do I describe myself to you? How can I explain that this is true?
I am who I am because of you! My work I love you !!

Comments and Discussions

 
QuestionWill this work in this scenario? Pin
iamlarph217-Feb-13 21:18
iamlarph217-Feb-13 21:18 
AnswerRe: Will this work in this scenario? Pin
S. M. Ahasan Habib17-Feb-13 22:20
professionalS. M. Ahasan Habib17-Feb-13 22:20 
GeneralRe: Will this work in this scenario? Pin
iamlarph217-Feb-13 23:27
iamlarph217-Feb-13 23:27 
GeneralRe: Will this work in this scenario? Pin
S. M. Ahasan Habib18-Feb-13 0:29
professionalS. M. Ahasan Habib18-Feb-13 0:29 
GeneralRe: Will this work in this scenario? Pin
iamlarph218-Feb-13 1:42
iamlarph218-Feb-13 1:42 
GeneralMy vote of 5 Pin
SRIRAM 26-Feb-13 0:02
SRIRAM 26-Feb-13 0:02 
GeneralRe: My vote of 5 Pin
S. M. Ahasan Habib7-Feb-13 0:30
professionalS. M. Ahasan Habib7-Feb-13 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.