Skip to content

LinqToSql With NOLOCK

June 9, 2014
tags: ,

This has been something I’ve been meaning to figure out how to do for ages, luckily I stumbled on a great solution by a smart guy named Omar Al Zabir.  You can find his full write up here:

http://msmvps.com/blogs/omar/archive/2009/03/07/linq-to-sql-solve-transaction-deadlock-and-query-timeout-problem-using-uncommitted-reads.aspx

Sometimes you just want a cheap call to populate some info pages, you know it’s a heavy call and you don’t want to lag anything up.  You can do it like this:

using (DeniedDataContext db = new DeniedDataContext())

{

db.ExecuteCommand(“SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;”); // The important line.

 

You’ll also need to extend your data context like so or you could start leaking connections.

public class DeniedDataContext : DCDBDataContext, IDisposable

{

public new void Dispose()

{

if (base.Connection != null)

if (base.Connection.State != System.Data.ConnectionState.Closed)

{

base.Connection.Close();

base.Connection.Dispose();

}

base.Dispose();

}

}

 

 

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: