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 698885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:19:31+00:00 2026-05-14T03:19:31+00:00

I am thinking of adding a DataContext as a member variable to my aspx.cs

  • 0

I am thinking of adding a DataContext as a member variable to my aspx.cs code-behind class for executing LinqToSql queries.

Is this thread safe? I am not sure if a new instance of this code-behind class is created for each HTTP request, or if the instance is shared amongst all request threads?

My fear is that I will get 10 simultaneous concurrent http requests that will be using the same database session.

public partial class MyPage : System.Web.UI.Page
{
    private DataContext myDB = new DataContext();

    protected void MyAction_Click(object sender, EventArgs e)
    {
        myDB.DoWork();
    }
} 
  • 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-14T03:19:31+00:00Added an answer on May 14, 2026 at 3:19 am

    The DataContext objects Thread Safety on MSDN is explained like this:

    Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

    Since your using a private member you have to consider your use in the class. As long as your not launching background threads and sharing the instance with you should have nothing to worry about.

    The DataContext should be treated as resource and you may want to consider explicitly disposing of it as part of your class lifecycle. Its a light-weight enough object to consider putting in a using block for every operation too, depending on your use.. see the MSDN description:

    a DataContext instance is designed to last for one “unit of work” however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

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

Sidebar

Related Questions

I have the following classes: public abstract class BaseClass { private readonly double cachedValue;
I know that this may sound like blasphemy to Lisp aficionados (and other lovers
This isn't a real fluent interface. I have an object which builds up a
I have a system that's similar to the Q&A feature of stackoverflow. The main
I am required to automate software tests for my job and I currently use
I have a form with a panel in which I want to display an
I have a select box, and I'd like to add a confirm before changing
I am starting to read about git-svn now to use it with a project
So lately I have been toying around with how Mathematica's pattern matching and term
I'm working on a Rails app using CanCan for RBAC and I only have

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.