Connection Overhead and Best Practice
Posted: 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
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