Click here to Skip to main content
Click here to Skip to main content

.NET TransactionScope and its default Transaction Isolation level issue

, 1 Feb 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
.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:

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

Or:

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.

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)

Share

About the Author

S. M. Ahasan Habib
Software Developer (Senior) The Jaxara IT Ltd.
Bangladesh Bangladesh
Mostly I work with MS technologies (ASP.NET MVC, WPF, C#, SQL Server, SSRS, SharePoint, Entity Framework, MSTest, Enterprise Library, MEF, WCF, WebAPI, MS Excel, IIS).
Non MS technologies which I love and use (Resharper, NHiberNet, JQuery, AngularJS, KnockoutJS, NodeJS, Python, MSpec, RihnoMock, Crystal Report, Subversion, Crome)

Comments and Discussions

 
QuestionWill this work in this scenario? Pinmemberiamlarph217-Feb-13 21:18 
AnswerRe: Will this work in this scenario? PinmemberS. M. Ahasan Habib17-Feb-13 22:20 
GeneralRe: Will this work in this scenario? Pinmemberiamlarph217-Feb-13 23:27 
GeneralRe: Will this work in this scenario? PinmemberS. M. Ahasan Habib18-Feb-13 0:29 
GeneralRe: Will this work in this scenario? Pinmemberiamlarph218-Feb-13 1:42 
GeneralMy vote of 5 PinmemberSRIRAM 26-Feb-13 0:02 
GeneralRe: My vote of 5 PinmemberS. M. Ahasan Habib7-Feb-13 0:30 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 1 Feb 2013
Article Copyright 2013 by S. M. Ahasan Habib
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid