Linq DataContext and Dispose
来源:互联网 发布:爱站seo 编辑:程序博客网 时间:2024/06/05 06:58
Linq DataContext and Dispose
A question many developers have with Linq and databases is this:
How important is it to call Dispose on a DataContext object?
Normally a quick web search gives an easy answer to common questions like this. But sometimes Google fails us. It turns out so many people have attempted to write about this with wrong or incomplete information that a web search returns mostly junk.
Here is a definitive answer, which I have finally learned after reading down to comment one hundred and twenty seven :) on a blog post by ScottGu, who runs the ASP.NET team at Microsoft.
Short answer: It is generally not critical to call Dispose on Datacontext.
Longer answer:
All objects in .NET are eventually disposed automatically by the automatic garbage collector. The reason developers are paranoid about calling it explicitly is that if an object contains an expensive resource like an open database connection, then we can't afford to wait around for garbage collection, it would have a big impact on scalability.
The good news is that DataContext objects do not keep open database connections like some ADO.Net objects do, so it doesn't really hurt to let the garbage collector do it for you.
There is one caveat here though: The DataContext does track change states for the data, and by disposing it you would release this memory more quickly. In almost all apps this difference wont be significant. However if you have a web site that is using all memory available this is one of many optimizations you could make.
Btw, a gigabyte of expensive ECC server memory is going for as little as 30 bucks now, so if your server is strained you can help it out pretty cheaply.
转载自:http://lee.hdgreetings.com/2008/06/linq-datacontex.html
- Linq DataContext and Dispose
- Linq to SQL DataContext
- Linq DataContext与实体
- Linq to sql(一) DataContext
- Linq 之DataContext与实体
- Connection Timeout Using LINQ DataContext
- Linq to sql: DataContext 隔离
- 是否需要手动执行DataContext的Dispose方法?
- 是否需要手动执行DataContext的Dispose方法?
- Linq to SQL:DataContext与实体
- Linq to sql(二):DataContext与实体
- linq DataContext.ExecuteQuery() 返回映射实体
- 解决LINQ多个DataContext查询问题
- 转三篇关于Linq datacontext的博客
- LINQ to SQL语句(22)之DataContext
- LINQ to SQL 之DataContext用法
- LINQ to SQL语句之DataContext
- DataContext
- H.264/AVC视频压缩编码标准的新进展
- C++中的引用
- 迈瑞霸王笔
- 1172. Queens, Knights and Pawns
- 每个程序员应该知道12件事
- Linq DataContext and Dispose
- 口语究竟怎么练?
- [JavaScript] In Velocity, CANNOT assign null to LRS
- 插入排序 python
- 基于P2P网络技术应用的思考
- [转]深入了解VC++编译器
- MiniGUI-1.6.10在ARM上的移植
- 完善Library的管理方式
- 架构师之路(7)---里氏代换原则 王泽宾