Click here to Skip to main content
6,822,123 members and growing! (18,121 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » LINQ » General     Intermediate License: The Code Project Open License (CPOL)

Change The Default CommandTimeout of LINQ DataContext

By S. M. SOHAN

An article that shows a simple way of changing the default value of the DataContext CommandTimeout
SQL, C#3.0, Windows, .NET3.5, ADO.NET, LINQ, Dev
Posted:22 May 2008
Views:19,517
Bookmarked:16 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 4.61 Rating: 4.61 out of 5

1
1 vote, 10.0%
2

3
1 vote, 10.0%
4
8 votes, 80.0%
5

Introduction

At times, the execution of a LINQ to SQL query may take a longer time and exceed the default value of the DataContext class's CommandTimeout property. The following article will present a way to achieve an application-wide CommandTimeout for your DataContext class.

The Problem

LINQ to SQL codes that invoke a long running database query/stored procedure end up with a System.Data.SqlClient.SqlException: Timeout expired Exception.

The Solution

The default value of the DataContext class's CommandTimeout is set to 30 seconds. Any database queries taking a longer time to complete than 30 seconds will throw a System.Data.SqlClient.SqlException: Timeout expired Exception.

One solution to this problem is to set the value of the CommandTimeout before each time a LINQ to SQL DataContext object is created and such a query is invoked. But the problem with this approach is, it will introduce code duplication and related issues.

We actually want to be able to change the default value of the CommandTimeout property and we want to do it efficiently. Fortunately enough, the Visual Studio 2008 auto generated DataContext subclasses provide an easy way to achieve this target using extensibility through partial methods.

The C# Code

Usually, an auto generated DataContext subclass has a few partial method declarations like the following:

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
//Other DataContext specific methods
#endregion

We are interested in the method called OnCreated() here. If you take a look into the constructors of the DataContext subclass, you will notice that this partial method is called from all the overloaded versions of the constructors in the end.

For those who are new to Partial Methods, please see this MSDN documentation. For a brief, partial methods are defined as extensibility points in a class. So, if there is no definition other than the declaration for that partial method [in another partial class], the call is just ignored. However, if there is an implementation, it will be invoked as a regular method.

So, we are going to define the body of the OnCreated() method in another partial class with the same full name as that of the auto generated class and set the desired timeout value there in the following way:

partial class SampleDBDataContext : System.Data.Linq.DataContext
{
    partial void OnCreated()
    {
        //Put your desired timeout here.
        this.CommandTimeout = 3600;

        //If you do not want to hard code it, then take it 
        //from Application Settings / AppSettings
        //this.CommandTimeout = Settings.Default.CommandTimeout;
    }
}

This is it! This is simple because:

  1. It remains so silent that the clients of the DataContext class do not need to take care of the CommandTimeout value.
  2. The change is only at a single place, which means no code duplication.
  3. Placing this method in a separate file in a partial class makes sure this change will persist although the auto generated class may be overwritten when a change takes place in the DBML file.

History

  • 22nd May, 2008: Initial post

License

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

About the Author

S. M. SOHAN


Member
I graduated on June, 2007 from Computer Science AND Engineering, BUET (Bangladesh University of Engineering and Technology).
Presently holding a job at Code71,Inc as a Senior Software Engineer.

I am a Scrum Alliance Certified Scrum Master and a Microsoft Certified Technology Specialist.

In future, I hope to take part in the IT decision making process for Bangladesh, my country, and contribute accordingly.

Read my tech blog at http://smsohan.blogspot.com
Occupation: Software Developer (Senior)
Company: Code71, Inc.
Location: Bangladesh Bangladesh

Other popular LINQ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralHelpful PinmemberbigRahn9:30 14 Jan '10  
GeneralRe: Helpful PinmemberS. M. SOHAN10:11 14 Jan '10  
GeneralNice one, thanks Pinmemberbakirankumar5:10 10 Dec '09  
GeneralNice. Pinmemberleforgeyahoo5:16 3 Feb '09  
GeneralRe: Nice. PinmemberS. M. SOHAN5:51 3 Feb '09  
GeneralRe: Nice. Pinmemberleforgeyahoo7:17 3 Feb '09  
GeneralRe: Nice. PinmemberSLaxman8:28 19 Aug '09  
GeneralRe: Nice. PinmemberSLaxman9:27 19 Aug '09  
GeneralNice one... thanks PinmemberPCoffey8:46 2 Feb '09  
GeneralRe: Nice one... thanks PinmemberS. M. SOHAN1:33 3 Feb '09  
General[Message Removed] PinmemberMojtaba Vali2:01 25 May '08  
GeneralRe: nice article PinmemberS. M. SOHAN19:33 26 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 22 May 2008
Editor: Deeksha Shenoy
Copyright 2008 by S. M. SOHAN
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project