如何使用 Entity Framework 4.1 了解 ASP.NET MVC3 应用程序当前打开了多少个连接
我有一个 MVC3 + Entity Framework 4.1 应用程序,目前我正在我的电脑上本地测试它。
我想知道我是否通过处理实体上下文正确关闭了所有连接。还有什么可用的方法可以让我看到当前打开的连接数。恐怕我没有正确关闭所有打开的连接。
或者有没有其他方法来检查我是否通过我的网络应用程序正确关闭了所有数据库连接
I am having an MVC3 + Entity Framework 4.1 app, currently iam testing it locally on my pc.
I want to know whether i have closed all my connections properly by disposing entities context or not. Also is there any available method thru which i can see how many connections are currently open. I am afraid i am not closing all my open connections properly.
Or is there any other way to check whether through my web app i am closing all my db connections properly or not
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 ADO.NET,通常您不需要手动打开/关闭物理数据库连接。有一个由框架处理的连接池。因此,当您执行 new SqlConnection 时,您并没有打开到数据库的新物理连接,而只是从池中提取一个连接。当您调用
connection.Close
时,您并没有关闭连接,您只是将其返回到连接池,以便可以重用它。因此,对您来说重要的是确保您的代码保留连接的时间尽可能短,并尽快将它们返回到池中。您可以查看以下文章,其中详细介绍了有关连接池。
With ADO.NET normally you do not open/close physical database connections manually. There is a connection pool handled by the framework. So when you do
new SqlConnection
you are not opening a new physical connection to the database, you are simply drawing one from the pool. And when you callconnection.Close
you are not closing the connection, you are simply returning it to the connection pool so that it can be reused.So what is important for you is to ensure that your code holds connections for as short time as possible and return them to the pool as fast as possible. You may take a look at the following article which goes into more details about connection pooling.