aspx.cs 代码隐藏文件中的 ASP.NET 线程安全

发布于 2024-08-26 01:53:03 字数 411 浏览 5 评论 0 原文

我正在考虑将 DataContext 作为成员变量添加到我的 aspx.cs 代码隐藏类中以执行 LinqToSql 查询。

这个线程安全吗?我不确定是否为每个 HTTP 请求创建此代码隐藏类的新实例,或者该实例是否在所有请求线程之间共享?

我担心我会同时收到 10 个使用相同数据库会话的并发 http 请求。

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

    protected void MyAction_Click(object sender, EventArgs e)
    {
        myDB.DoWork();
    }
} 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2024-09-02 01:53:03

DataContext 对象 MSDN是这样解释的:

此类型的任何公共静态(在 Visual Basic 中为共享)成员都是线程安全的。不保证任何实例成员都是线程安全的。

由于您使用私有成员,因此您必须考虑在课堂上的使用。只要您不启动后台线程并与您共享实例,就无需担心。

DataContext 应被视为资源,您可能需要考虑将其显式处置为类生命周期的一部分。它是一个足够轻量级的对象,可以考虑为每个操作添加一个 using 块,具体取决于您的使用情况。请参阅 MSDN 说明

DataContext 实例被设计为持续一个“工作单元”,但您的应用程序定义了该术语。 DataContext 是轻量级的并且创建起来并不昂贵。典型的 LINQ to SQL 应用程序在方法范围内创建 DataContext 实例,或者作为表示一组相关数据库操作的逻辑集的短期类的成员。

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.

木森分化 2024-09-02 01:53:03

它是安全的,因为每次加载页面时都会创建一个新的 MyPage 实例,因此也会创建一个新的 DataContext 实例。

无需担心线程安全问题。

It's safe because each time your page is loaded a new instance of MyPage is created, thus a new instance of your DataContext is also created.

There's no thread safety issue to worry about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文