ASP.NET Thread Safety in aspx.cs code behind file

Posted by Tim Michalski on Stack Overflow See other posts from Stack Overflow or by Tim Michalski
Published on 2010-03-24T17:36:50Z Indexed on 2010/03/24 17:43 UTC
Read the original article Hit count: 222

Filed under:
|
|

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();
    }
} 

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about datacontext