LINQ datacontext for web application management
I am reading Multiple / One Instance Linq to SQL DataContext and http://www.west-wind.com/weblog/posts/246222.aspx
I am developing a web application. In my blclass, which has many methods that make some query to the DB, in each method, I declare:
Dim db As New DemoDataClassesDataContext
to create a new DataContext instance for each transaction. I am wondering if this would be a problemwith performance or any other issues that I am not aware of. Is it the right thing to do?
Should I use:
Dim db As New DemoDataClassesDataContext or Using db As New DemoDataClassesDataContext
I understand that all objects in .NET end up being automatically cleaned up by the automatic garbage collector.
The using statement must dispose of the object as soon as the code between the statement has been executed.
This blog post http://lee.hdgreetings.com/2008/06/linq-datacontex.html states that " It is generally not critical to call Dispose on a Datacontext. "
I don't have a clear picture yet on how to manage the LINQ datacontext liftime for a web application. Any suggestion is greatly appreciated.
Thanks.
a source to share
The using statement must dispose of the object as soon as the code between the statement has executed.
This is not true. After that, the code will be called at the end of the object when the code goes out of scope. This in turn will close the underlying database connection. Thus, you need a using statement to make sure the database connection is closed.
EDIT:
In fact, you don't need to call dispose on datacontract if you are listing the entire result set as described here: When do I need to dispose of the data context
a source to share