« IP Blocker Updated version 0.84 (IISIP) | Main | DreamScene for Vista - Tropical Ocean Waves (HD 1080p) »

June 24, 2008

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54fc3203e883400e5536dd36e8833

Listed below are links to weblogs that reference Linq DataContext and Dispose:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Raj

Nice article,
I still face one problem. Sometime i get error is, Data Reader is already open. Im using LINQ to SQL. and im using static object of DataContext. is it right approch to use Static Datacontext Variable?

Raj

Invalid attempt to call Read when reader is closed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Invalid attempt to call Read when reader is closed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: Invalid attempt to call Read when reader is closed.]
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +812
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +129
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +878
System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +106
System.Linq.Queryable.FirstOrDefault(IQueryable`1 source) +267
Xpertz.BLL.cCity.GetCityById(Int32 cityId) in F:\Hill\Xpertz\BLL\cCity.cs:88
Xpertz.BLL.cMenu.GetMenuForCity(Int32 cityId) in F:\Hill\Xpertz\BLL\cMenu.cs:10
Hill.City.Page_Load(Object sender, EventArgs e) in F:\Hill\Hill\City.aspx.cs:21
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436


John Rusk

Thanks for digging into the details of this issue, and documenting the results.

I had a look for the ScottGu post. Was this the one: http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx ?

Pieter Jouber

There is one error in your logic.
You say the garbage collector always eventually dispose objects.
This is incorrect, it only does so most of the time.

If the process (program) is removed from memory, (E.G you close a windows app) the whole program is removed from memory without going through normal garbage collections. If the only recources is memory recources it is no problem, but SQL server connections can be left open.

A bigger problem is if you use ASP.Net each request is treated like this and you can end up using the maximum number of connections very quickly if the connections is not disposed.

To mitigate this with linq, linq to entities and linq to sql will usually open the connection when it needs it and close it immediatly afterwards. So your SQL connections don't leak even when you don't dispose the context.

There is one more problem with the linq scenario, sometimes the connection is not closed immediatly after a call to a quary, because the result object is still available, now this will be disposed by the garbage collector, but if the request close before it is finally disposed there can still be a leak of connections.

The moral of the story, it will probably be ok not to dispose a data context, but to be save you need to dispose it.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment