我是否正确使用 DataContext.Dispose() ?
我在 ASP.NET MVC 项目中遇到了大量连接池超时问题。我一直在读到,尽管 Linq-to-SQL 应该为我进行处置,但它并不总是有效,而且不手动处置继承 IDisposable 的任何内容都是不好的做法。
我正在使用所有 Linq-to-SQL 语句所使用的存储库模式。由于不知道将 DataContext.Dispose()
方法放在哪里,我将其放在由两行代码组成的 SubmitChanges()
函数中:
public void SubmitChanges()
{
db.SubmitChanges();
db.Dispose();
}
这是一个好地方吗?这样做还是我做的完全错误?
I've been getting lots of connection pool timeouts in my ASP.NET MVC project. I've been reading that even though Linq-to-SQL should be disposing for me it doesn't always work and also that not manually disposing anything that inherits IDisposable
is bad practice.
I'm using the repository pattern that all my Linq-to-SQL statements use. Not knowing where to put the DataContext.Dispose()
method, I put it in the SubmitChanges()
function that consists of two lines of code:
public void SubmitChanges()
{
db.SubmitChanges();
db.Dispose();
}
Is this a good place to do this or am I doing it completely wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它实现 IDisposable,最简单的方法是:
这确保它在适当的时间被处置。
这样您就不必担心它会挂起,并且不会因为异常等而被调用。
dispose 关键字(MSDN 链接)。
由于它们是相关的,这里有一个关于Dispose and Finalize的链接。听起来在这种情况下,您想要实现存储库,以便它实现 IDisposable。这样调用对象就可以创建它,执行它需要执行的操作并关闭它。然后,您可以在处理/完成数据上下文时清理数据上下文。
If it implements IDisposable the easiest way to do it is:
This ensures it is disposed at the appropriate time.
This way you don't have to worry about it hanging around, and not getting called because of an exception etc.
The dispose key word (MSDN Link).
Since they are related, here is a link about Dispose and Finalize. It sounds like in this case, you want to implement the Repository so that it implements IDisposable. This way the calling object can create it, do what it needs to do and close it. You then can clean up the data context when it is disposed/finalized.
经过更多挖掘后,我发现了这篇文章:
http://stephenwalther.com/blog/archive/2008/08/20/asp-net-mvc-tip-34-dispose-of-your -datacontext-or-don-t.aspx
并在评论部分 Craig Stuntz 写道:
因此,我按照 Craig 的建议进行操作,并覆盖了
Controller
继承的 Dispose 方法。在我的控制器代码的顶部:
在我的存储库中,我有一个名为 Dispose 的方法,如下所示:
其中 db 是我的
DataContext
。现在我的重写 Dispose 方法每次都会被调用:)并且我不必将所有
ActionResult
包装在 using 块中Well after some more digging I came across this post:
http://stephenwalther.com/blog/archive/2008/08/20/asp-net-mvc-tip-34-dispose-of-your-datacontext-or-don-t.aspx
and in the comments section Craig Stuntz wrote:
So I did what Craig suggested and overrode the Dispose method that the
Controller
inherits.At the top of my controller code:
and in my Repository I have a method called Dispose that looks like:
where db is my
DataContext
.Now my overriden Dispose method gets called every time :) and I don't have to wrap all my
ActionResult
in using blocksDataContext
(您的存储库)必须实现IDisposable
。理想情况下,您需要一个
UnitOfWork
来传递到存储库并实现IDisposable
。在这里,您将其留给客户端调用另一个不好的方法。DataContext
(your repository) must implementIDisposable
.Ideally you need a
UnitOfWork
to pass to repositories and that implementsIDisposable
. Here you are leaving it down to the client to call another method which is not good.