简体   繁体   English

asp.net-mvc-4应用程序中的ADO.Net SQL池

[英]ADO.Net SQL pooling in asp.net-mvc-4 application

I want to optimize the sql connection in my wep application its create in .net mvc 4, i read that ado.net automatically manager the connection pooling but i'm some lost respect how exactly implement that, is correct if i create a global object with the connection in the Application_Start class then pass the connection object through all data object in my application ? 我想优化我的wep应用程序中的sql连接,并在.net mvc 4中创建它,我读到ado.net自动管理连接池,但是我有些失落了如何正确实现连接池,如果我创建了一个全局对象,那是正确的然后通过Application_Start类中的连接将连接对象通过我的应用程序中的所有数据对象传递? something like this 像这样的东西

protected void Application_Start()
{
...
SqlConnection conn = new SqlConnection("Connection String...");
DAOPeople daoPeople = new DAOPeople(conn);
...
}

in that way i avoided create a new SqlConnection for each dao, is correct? 这样,我避免为每个dao创建一个新的SqlConnection,对吗?

No, don't do that. 不,不要那样做。 You'll end up with a bottleneck at your connection object, as that single connection is shared across all sessions and requests to your app. 您最终将在连接对象上遇到瓶颈,因为单个连接在所有会话和对应用程序的请求之间共享。

For connection pooling, you do the exact opposite: don't try to share or re-use a single connection object; 对于连接池,您执行的操作正好相反: 不要尝试共享或重用单个连接对象; 不要尝试共享一个连接对象。 do just create a new SqlConnection every time you need it, open it on the spot, and make sure it's disposed as soon as you're done via a using block. 只是创建一个 SqlConnection每次你需要它的时候,打开它,在现场,并确保它只要你通过做配置using块。 Even though your code looks like you're opening and closing a lot of connections, the connection pooling feature is built in and ensures you keep drawing from a small number of existing connections in the same pool. 即使您的代码看起来像要打开和关闭许多连接,连接池功能也是内置的 ,可确保您从同一池中少量现有连接中进行绘制。

That said, if you're on a really large site, you can do a little better. 这就是说,如果你是一个真正的大网站,你可以做的更好一点。 One thing large sites will do to help scale is avoid unnecessary memory allocations, and there is some memory that goes with creating an SqlConnection object. 大型站点为帮助扩展而要做的一件事是避免不必要的内存分配,并且创建SqlConnection对象会附带一些内存。 Instead they might, for example, have one main SqlConnection per HTTP request, with the possibility of either enabling MARS or having an additional secondary connection object in the request so they can run some things asynchronously. 相反,例如,每个HTTP请求可能具有一个主SqlConnection ,从而有可能启用MARS或在请求中具有其他辅助连接对象,以便它们可以异步运行某些内容。 But this is only something the top 0.1% need to care about, and if you're at this level you're measuring to find out where the proper balance is for your particular site and load. 但是,这只是0.1%最高的用户需要关心的事情,如果您处于此级别,则需要进行测量以找出适合您的特定站点和负载的适当平衡位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM