Connection Overhead and Best Practice

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Chill
Posts: 1
Joined: Fri 22 Aug 2014 19:32

Connection Overhead and Best Practice

Post by Chill » Fri 22 Aug 2014 19:42

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

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Connection Overhead and Best Practice

Post by MariiaI » Tue 26 Aug 2014 12:39

Is there a specific disadvantage to keeping the same data context alive for a long period?
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.

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.

Post Reply