简体   繁体   English

在数据库调用之间共享ASP.NET Web App中的SqlConnection对象

[英]Share SqlConnection object in ASP.NET Web App Between DB calls

是否有一种方法可以有效地在至少来自同一页面或同一请求或整个应用程序的调用之间共享单个SqlConnection实例?

If you are using ADO.NET or any framework that makes use of ADO.NET , then it is likely already connection pooling for you. 如果您正在使用ADO.NET或利用任何框架ADO.NET ,那么它很可能已经连接池为您服务。

What database are you using? 您正在使用什么数据库? - connection pooling could possibly vary depending on the type of database. -连接池可能会根据数据库类型而有所不同。

You could always verify this by monitoring your database server connections. 您始终可以通过监视数据库服务器连接来验证这一点。

For example, on SQL Server, sp_who2 will list all of the connections. 例如,在SQL Server上, sp_who2将列出所有连接。 Look for the connection(s) from your machine and notice that the spid (unique identity for each connection) stays the same between your web application page loads. 在您的计算机上查找连接,并注意到在Web应用程序页面加载之间, spid (每个连接的唯一标识)保持不变。

If you want to know how to do this, the answer is by declaring a static variable of type SqlConnection inside the page and use it for your database access inside all of your page methods; 如果您想知道如何做到这一点,答案是通过在页面内部声明一个SqlConnection类型的静态变量,并将其用于所有页面方法中的数据库访问。 however, THIS IS A TERRIBLE IDEA and the reasons are many: 但是, 这是一个可怕的想法 ,原因很多:

  1. As many people have pointed out, SQLConnections are pooled and there's no need to try an avoid opening a connection for every database access. 正如许多人指出的那样,SQLConnections是池化的,因此无需尝试避免为每个数据库访问打开连接。 There won't be any performance gains by doing so. 这样做不会提高性能。

  2. Remember that every request made to your applications will be served by a different Thread taken from the Application Pool. 请记住,对您的应用程序的每个请求都将由来自应用程序池的不同线程来处理。 If you have a static variable like this, 2 concurrent requests to the same page may end up accessing the same SqlConnection instance and you could be caught in a scenario in which while the second request is retrieving data from the DB, the first request may close the connection unexpectedly. 如果您有这样的静态变量,则对同一页面的2个并发请求可能最终访问同一SqlConnection实例,并且您可能会遇到以下情况:第二个请求正在从数据库中检索数据时,第一个请求可能会关闭连接意外。

  3. You should perform the data access operations at a different layer of your application's architecture. 您应该在应用程序体系结构的不同层上执行数据访问操作。 A good approach is to have a Business Layer that has a reference to an application Data Access Layer which in turn is in charge to perform the data access operations. 一个好的方法是拥有一个业务层,该业务层引用一个应用程序数据访问层,而该应用程序又负责执行数据访问操作。 This provides greater encapsulation, separation of concerns and increases maintainability, reusability, etc. Here's a good article explaining the idea. 这提供了更大的封装,关注点分离并提高了可维护性,可重用性等。 这是一篇很好的文章,对这一想法进行了说明。

That's a really bad idea. 那真是个坏主意。 SqlConnection implements IDisposable which means you must dispose of it to free resources as soon as possible. SqlConnection实现IDisposable ,这意味着您必须将其处置以尽快释放资源。

As has already been mentioned, under the hood connection pooling already optimises the creation of connections. 如前所述,引擎盖下的连接池已经优化了连接的创建。 If you start holding on to connections you could end up with serious problems, I see it time and time again . 如果您开始保持连接,可能会遇到严重的问题,我一次又一次看到它。

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

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