I am wondering what the best practices are for creating datacontext connections in a large project.
I have seen some people create data context objects in each method it is required in, e.g.
void TestMethod()
{
using (ExampleDataContext dc = new ExampleDataContext(connectionString)) {
dc.foo;
}
}
And also where a single connection is created for a class and left open for the duration of the classes lifetime.
Does the first have a particular performance overhead creating and closing connections or are the physical database connections kept alive in the driver anyway?
Is there a specific disadvantage to keeping the same data context alive for a long period?
Thanks
Connection Overhead and Best Practice
Re: Connection Overhead and Best Practice
Generally, we don't recommend using a static instance of the DataContext, due to the fact that this instance will accumulate obsolete data (the data is cached after it has been received, and when working for a time with one DataContext, the data in the database may change while the data in the cache will be obsolete). DataContext is a lightweight object, and it is recommended to use new DataContext for each unit of work instead of keeping it for a long time.Is there a specific disadvantage to keeping the same data context alive for a long period?
Please refer to:
http://www.devart.com/linqconnect/docs/?Lifecycle.html
http://www.devart.com/linqconnect/docs/ ... tions.html
If you have any further questions, feel free to contact us.