LINQ上下文应该每次都关闭吗?

时间:2022-09-24 18:00:30

In LINQ to SQL, is it necessary to close the context after performing a select on the database (and of course, after consuming the data)? if I leave it open, doesn't it mean that the connection to the server is left open?

在LINQ to SQL中,是否有必要在对数据库执行select之后关闭上下文(当然,在使用数据之后)?如果我把它打开,是不是意味着与服务器的连接保持打开状态?

Thanks, Lucian

4 个解决方案

#1


The underlying connection is not closed and disposed until you close the DataContext, using Dispose method. You should always call Dispose after using the DataContext. Think of the DataContext as a traditional Connection object, that is more or less what it is under the hood.

在使用Dispose方法关闭DataContext之前,不会关闭和释放基础连接。在使用DataContext之后,应始终调用Dispose。将DataContext视为传统的Connection对象,或多或少是它的底层。

#2


Q. How long does my database connection remain open?

问:我的数据库连接保持打开状态多长时间了?

A. A connection typically remains open until you consume the query results. If you expect to take time to process all the results and are not opposed to caching the results, apply ToList(TSource) to the query. In common scenarios where each object is processed only one time, the streaming model is superior in both DataReader and LINQ to SQL.

答:在您使用查询结果之前,连接通常保持打开状态。如果您希望花时间处理所有结果并且不反对缓存结果,请将ToList(TSource)应用于查询。在每个对象只处理一次的常见场景中,流模型在DataReader和LINQ to SQL中都是优越的。

The exact details of connection usage depend on the following:

连接使用的确切细节取决于以下内容:

Connection status if the DataContext is constructed with a connection object.

如果DataContext是使用连接对象构造的,则为连接状态。

Connection string settings (for example, enabling Multiple Active Result Sets (MARS). For more information, see Multiple Active Result Sets (MARS).

连接字符串设置(例如,启用多个活动结果集(MARS)。有关详细信息,请参阅多个活动结果集(MARS)。


from MSDN LINQ to SQL FAQ

从MSDN LINQ到SQL FAQ

#3


I am a bit uncertain about the names of everything in LiNQ. In entity framework it means that the object context is trying to do change tracking which in case of a disconnected scenario causes alot of problems.

我对LiNQ中所有东西的名字都有点不确定。在实体框架中,它意味着对象上下文正在尝试进行更改跟踪,在出现断开连接的情况时会导致很多问题。

ASP.NET is unfortunately not very compatible with change tracking today so closing the context after usage is the best way to handle entity framework in asp.net. If I try to leave tyhe context open I sometimes have problems updating entities I believe it should be the same issue in Linq.

遗憾的是,ASP.NET今天与变更跟踪不太兼容,因此在使用后关闭上下文是在asp.net中处理实体框架的最佳方式。如果我试图让tyhe上下文打开,我有时会在更新实体时遇到问题我认为它应该是Linq中的同一个问题。

In a Windows Forms application with the database on the same machine or in a local network I would reuse the object context through a facade (less code duplication) to be able to benefit from the change tracking and direct connection to the database.

在具有同一计算机或本地网络中的数据库的Windows窗体应用程序中,我将通过外观重用对象上下文(减少代码重复),以便能够从更改跟踪和直接连接到数据库中受益。

#4


The short answer is yes, you should open a datacontext only when it's needed and close the it as soon as possible. If you don't dispose of it, the underlying database connection may stay open.

简短的回答是肯定的,你应该只在需要时打开一个datacontext并尽快关闭它。如果您不处置它,底层数据库连接可能保持打开状态。

There have been many other people asking similar questions already. You'll find some pretty good discussions if you check some of them out.

还有很多人已经提出过类似的问题了。如果你检查一下,你会发现一些非常好的讨论。

#1


The underlying connection is not closed and disposed until you close the DataContext, using Dispose method. You should always call Dispose after using the DataContext. Think of the DataContext as a traditional Connection object, that is more or less what it is under the hood.

在使用Dispose方法关闭DataContext之前,不会关闭和释放基础连接。在使用DataContext之后,应始终调用Dispose。将DataContext视为传统的Connection对象,或多或少是它的底层。

#2


Q. How long does my database connection remain open?

问:我的数据库连接保持打开状态多长时间了?

A. A connection typically remains open until you consume the query results. If you expect to take time to process all the results and are not opposed to caching the results, apply ToList(TSource) to the query. In common scenarios where each object is processed only one time, the streaming model is superior in both DataReader and LINQ to SQL.

答:在您使用查询结果之前,连接通常保持打开状态。如果您希望花时间处理所有结果并且不反对缓存结果,请将ToList(TSource)应用于查询。在每个对象只处理一次的常见场景中,流模型在DataReader和LINQ to SQL中都是优越的。

The exact details of connection usage depend on the following:

连接使用的确切细节取决于以下内容:

Connection status if the DataContext is constructed with a connection object.

如果DataContext是使用连接对象构造的,则为连接状态。

Connection string settings (for example, enabling Multiple Active Result Sets (MARS). For more information, see Multiple Active Result Sets (MARS).

连接字符串设置(例如,启用多个活动结果集(MARS)。有关详细信息,请参阅多个活动结果集(MARS)。


from MSDN LINQ to SQL FAQ

从MSDN LINQ到SQL FAQ

#3


I am a bit uncertain about the names of everything in LiNQ. In entity framework it means that the object context is trying to do change tracking which in case of a disconnected scenario causes alot of problems.

我对LiNQ中所有东西的名字都有点不确定。在实体框架中,它意味着对象上下文正在尝试进行更改跟踪,在出现断开连接的情况时会导致很多问题。

ASP.NET is unfortunately not very compatible with change tracking today so closing the context after usage is the best way to handle entity framework in asp.net. If I try to leave tyhe context open I sometimes have problems updating entities I believe it should be the same issue in Linq.

遗憾的是,ASP.NET今天与变更跟踪不太兼容,因此在使用后关闭上下文是在asp.net中处理实体框架的最佳方式。如果我试图让tyhe上下文打开,我有时会在更新实体时遇到问题我认为它应该是Linq中的同一个问题。

In a Windows Forms application with the database on the same machine or in a local network I would reuse the object context through a facade (less code duplication) to be able to benefit from the change tracking and direct connection to the database.

在具有同一计算机或本地网络中的数据库的Windows窗体应用程序中,我将通过外观重用对象上下文(减少代码重复),以便能够从更改跟踪和直接连接到数据库中受益。

#4


The short answer is yes, you should open a datacontext only when it's needed and close the it as soon as possible. If you don't dispose of it, the underlying database connection may stay open.

简短的回答是肯定的,你应该只在需要时打开一个datacontext并尽快关闭它。如果您不处置它,底层数据库连接可能保持打开状态。

There have been many other people asking similar questions already. You'll find some pretty good discussions if you check some of them out.

还有很多人已经提出过类似的问题了。如果你检查一下,你会发现一些非常好的讨论。