数据库服务器太忙。打开 DataContext 所需的建议

发布于 2024-11-27 21:00:24 字数 1422 浏览 0 评论 0原文

我这个庞大的系统昨天开始运行。数千人同时连接..我正在使用 SQL Server 2008 和 .NET 3.5 (C#);

今天我注意到我的数据库服务器变得非常慢(顺便说一句,服务器机器真的非常能够轻松处理这个应用程序的 10 倍,所以我一定搞乱了我的连接或其他什么)。

首先:我的业务类每个都创建唯一的一个 DataContext。在类级别上,就像这样:

public class BusProduct : IDisposable
{
    //DataContext
    DBDataContext db = new DBDataContext();


    //Variable of type DAL.Products (that will be set on application level)
    public Products _attributes { get; set; }

    //Dispose
    public void Dispose()
    {
        Dispose(true);

        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
    }

    /// Constructor
    public BusProduct()
    {
        try
        {
            //New object
            _attributes = new Product();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message,ex);
        }
    }

    public void Insert()
    {
        try
        {
            //Insert data set on _attributes
            db.Products.InsertOnSubmit(_attributes);
            db.SubmitChanges();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message,ex);
        }

    }

}

你们对此有何看法(每个类一个 DataContext)?你有什么建议?对于大班来说有好处吗?还好吗?

我喜欢这样做主要是因为像 IQueryable 这样的返回。 (当尝试访问这种返回时,如果我为每个方法使用一个 DataContext,它将已经被释放)

I have this huge system started running yesterday. Thousands of people connecting simultaneously.. I'm Using SQL Server 2008 and .NET 3.5 (C#);

Today I noticed my db server was getting really slow (btw the server machine is really, really capable of handling like 10x this application with ease, so I must be messing up my connections or something).

First of all: My Business classes are creating the only one DataContext each. On class level, like this:

public class BusProduct : IDisposable
{
    //DataContext
    DBDataContext db = new DBDataContext();


    //Variable of type DAL.Products (that will be set on application level)
    public Products _attributes { get; set; }

    //Dispose
    public void Dispose()
    {
        Dispose(true);

        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
    }

    /// Constructor
    public BusProduct()
    {
        try
        {
            //New object
            _attributes = new Product();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message,ex);
        }
    }

    public void Insert()
    {
        try
        {
            //Insert data set on _attributes
            db.Products.InsertOnSubmit(_attributes);
            db.SubmitChanges();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message,ex);
        }

    }

}

What do you guys think about this (One DataContext per class)? What would you suggest? Is it good for huge classes? Is it even good?

I Like to make it like this mainly because of returns like IQueryable<Type>. (When trying to access this kind of return, if I use one DataContext for each method, it will already be disposed)

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

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

发布评论

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

评论(1

半城柳色半声笛 2024-12-04 21:00:24

你们对此有何看法(每个类一个 DataContext)?

你所做的事情在很多层面上都是错误的,这并不有趣——例如,如何在事务中同步多个类的更新?分布式事务 200 次更新只是因为您不关心正确的连接处理?这是非常昂贵的。您如何处理多个类之间的连接和项目?您强制使用不同的数据上下文(每个类一个),这将导致底层提供程序完全无法优化 - 并不是说​​这在技术上是不可能的,但我确信开发人员在被问到为什么时会告诉您正确地起诉这些东西不关心它。

这将需要完全重写才能采用合理的方法 - 在页面启动时打开连接,在页面结束时关闭连接。好吧,如果您需要单独的数据库事务,有时会不止一个,但通常 - 不需要。

另外,获取一本向您介绍存储库模式的初学者书籍。我们通过 DataManager 运行所有数据库访问。

DataManager包含连接,实现类似Entity的东西,是IQueryable。一开始打开一个,根据需要使用它,完成。

What do you guys think about this (One DataContext per class)?

What you do is wrong on so many levels it isn't fun - for example, how do you sync updates on multiple classes in a transaction? A distributed transaction for 200 updates JUST because you don't care about proper connection handling? This is very expensive. How do you deal with joins and projects between multiple classes? You force different data contexts (one per class) which will result in a total inability of the underlying provider to optimize - not that it is technically impossible, but I am sure the developer will just tell you to sue the stuff properly when asked why it does not take care of it.

This will require a total rewrite to get into a sensible approach - open a connection when the page starts, close it when the page ends. Well, sometimes more than one if you need separate database transactions, but generally - no.

Plus get one of those beginner books telling you about the repository pattern. We run all db access throuh a DataManager.

DataManager contains connection, implements stuffl like Entity which is IQueryable.Open one at the start, use it as you need, finished.

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