"Invalid attempt to call Read when reader is closed" in LINQ DataContext
I got a lot of error from a LINQ DataContext, such as "Invalid attempt to call Read when reader is closed" . That is because I was using a singleton for DataContext in a multi-threads.
The codes like that:
Code
private static ExampleDataContext Context; | |
private static ExampleDataContext GetContext() | |
{ | |
if(Context == null) | |
{ | |
Context = new ExampleDataContext(); | |
} | |
return Context; | |
} |
That is not very thread-safe. A thread will close the DataContext, but others threads are still using DataContext. Then it will throw the error above.
I changed the DataContext in a pre-thread. I initialize the DataContext in each thread. The errors are gone! You also can do the DataContext in a pre-object. That is simpler.
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
Feedback awaiting moderation
This post has 3949 feedbacks awaiting moderation...
Form is loading...