Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:20:18+00:00 2026-05-25T02:20:18+00:00

We create one Linq2Sql DataContext for every request using the following class: public static

  • 0

We create one Linq2Sql DataContext for every request using the following class:

public static class DataContextManager
{

    private const string HTTPCONTEXT_KEY = "DataContextManagerKey";

    private static CompanyDataContext _staticContext; //when there's no HttpContext (in test/debug situations). 

    public static CompanyDataContext Context
    {
        get
        {
            if (_Context == null)
            {
                _Context = NewContext();
            }
            return _Context;
        }
    }

    private static CompanyDataContext _Context
    {
        get
        {
            return (CompanyDataContext)(HttpContext.Current != null ? HttpContext.Current.Items[HTTPCONTEXT_KEY] : _staticContext);
        }
        set
        {
            if (HttpContext.Current != null)
            {
                HttpContext.Current.Items[HTTPCONTEXT_KEY] = value;
            }
            else
            {
                DataContextManager._staticContext = value;
            }    
        }
    }

    public static void Dispose()
    {
        CompanyDataContext context = _Context;
        if (context != null)
        {
            if (Config.Instance.TestMode) context.Log.WriteLine("--- DISPOSING DATACONTEXT ---");
            context.Dispose();
            _Context = null;
        }
    }

    public static CompanyDataContext NewContext()
    {  
        CompanyDataContext db = new CompanyDataContext();
        db.CommandTimeout = Config.SQL_COMMAND_TIMEOUT;
        if (Config.Instance.TestMode)
        {
            db.Log = new ConsoleTextWriter();
            db.Log.WriteLine("--- CREATING NEW DATACONTEXT ---");
        }
        return db;
    }

}

And in Global.asax:

protected void Application_EndRequest(Object sender, EventArgs e)
{
    DataContextManager.Dispose();
}

The reason why I’m asking is that we’re suddenly getting random “SqlException: Server failed to resume the transaction” exceptions once or twice every day with code that used to work perfectly. After the exception we get a lot of other exceptions until we restart the web application. Anyone seen this behaviour before?

We’re running ASP .Net 2.0 with SQL server 2005 on IIS 6.

UPDATE:

Just so no one else does the same horrible mistake as we did:

It turned out that some worker threads also used the DataContext but with no HttpContext they of course got the _staticContext (a feature in DataContextManager only to be used when testing). We rewrote the code in the worker threads to make sure one DataContext per thread and disposing it when done. And so far everything has worked for 2 weeks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:20:18+00:00Added an answer on May 25, 2026 at 2:20 am

    This is a bad pattern. First you should never have a static data context it implements IDisposable so one thread could try to use the context while another is disposing of it plus many other potential problems. One data context per http request is no good either, data contexts are designed to be used for a single transaction then disposed of. You get problems if you retrieve update/insert/delete and retrieve using the same context, the second retrieve does not reflect the changes of the update/insert/delete. Remove the static context and just have the Context property return a new context every time. You could still dispose of the all at the end of a request by sticking them all in a List property and iterating through it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would one create a Singleton class using PHP5 classes?
I want to create one simple application for sending request from iphone(client) by using
I have to create one table using XSLT and CSS. The table should look
I wanted to create one js file which includes every js files to attach
Should one create unit tests involving IO? Ie, testing a class method for serializing/deserializing
I'm developing an application which will have these classes: class Shortcut { public string
is it better to create one resource for every asp.net page application. or to
I often take the classes that linq2sql generates and create a simple data-only class
I have to create one table using XSLT and CSS. The table should look
I have recently inherited an ASP.Net app using Linq2SQL. Currently, it has its DataContext

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.