实体框架4没有关闭sql server 2005分析器中的连接 [英] Entity framework 4 not closing connection in sql server 2005 profiler

查看:109
本文介绍了实体框架4没有关闭sql server 2005分析器中的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.net应用程序中首次使用实体框架4。
我想确保在离开使用语句后关闭DB连接,但是在SQL Server 2005分析器中,在离开使用时看不到连接注销,只有输入登录时才能看到连接注销。



例如,我从一个空白的asp.net页面开始,在Page_Load中,我尝试使用这个简单的代码(绝对没有别的页面):

  using(var model = new DB(name = DB))
{
var livre =(from l在model.books
中选择l).SingleOrDefault();
}

在分析器中,我看到审核登录,但不是离开后退出使用。
所以我试图使用以下clode明确地关闭连接:

  using(var model = new DB( name = DB))
{
var livre =(from l in model.books
select l).SingleOrDefault();
model.Connection.Close();
}

再次看到登录,而不是注销。奇怪的是,当我在关闭后检查model.Connection中的ConnectionState属性时,它表示已关闭,但不在Sql Server中。



要查看实际在SQL Server中注销,我必须按下VS2010中的停止调试按钮,当我重新启动Web应用程序时,只有在我看到注销(接下来是我的页面被处理后的实际登录)。



我开始怀疑SQL Server中是否存在某些可能会使连接打开,即使在代码中已关闭。



任何想法?



谢谢

解决方案

是的,将保持连接打开的SQL Server:连接池。由于打开和关闭数据库连接是一项昂贵的操作,SQL Server会管理它将分配给连接请求的连接池。大致来说,如果请求连接字符串与池中的非活动连接的连接字符串相匹配,则SQL Server将分配连接到请求。


I'm using entity framework 4 for the first time in an ASP.net application. I wanted to make sure that DB connections are closed after leaving the using statement, however, in the SQL Server 2005 profiler, I cannot see the connection logout when leaving the using, only the login when entering it,

For example, I started with a blank asp.net page, and in the Page_Load, I tried with this simple code (and absolutly nothing else in the page) :

using (var model = new DB("name=DB"))
{
  var livre = (from l in model.books
           select l).SingleOrDefault();
}

In the profiler, I see the Audit login, but not the logout after leaving the "using". So I tried closing the connection explicitly, using the following clode:

using (var model = new DB("name=DB"))
{  
  var livre = (from l in model.books
           select l).SingleOrDefault();
  model.Connection.Close();
}

Again, I see the login, but not the logout. The strange thing is that when I inspect the ConnectionState property in model.Connection after the close, it indicates 'Closed', but not in Sql Server.

To see the actual logout in SQL Server, I have to press the Stop debugging button in VS2010, and when I restart the Web Application, only then I see the logout (following by the actual login when my page gets processed).

I'm starting to wonder if there is something in SQL Server that might keep the connection opened even if it's closed in the code.

Any idea?

Thanks

解决方案

Yes, there is something in SQL Server that will keep the connection open: connection pooling. As opening and closing database connections is an expensive operation, SQL Server manages a pool of connections that it will assign to connection requests. Roughly speaking, if the request connection string matches the connection string of a inactive connection in the pool, SQL Server will assign the connection to the request.

这篇关于实体框架4没有关闭sql server 2005分析器中的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆