Opinion on reuse of db context in Linq

Microsoft provides the following advice/recommendation to not reuse DataContext instances http://msdn.microsoft.com/en-us/library/bb386929.aspx

Frequently Asked Questions (LINQ to SQL)

Connection Pooling

Q. Is there a construct that can help with DataContext pooling?

A. Do not try to reuse instances of DataContext. Each DataContext maintains state (including an identity cache) for one particular edit/query session. To obtain new instances based on the current state of the database, use a new DataContext.

You can still use underlying ADO.NET connection pooling. For more information, see SQL Server Connection Pooling (ADO.NET).


It is ok to reuse for different parts of the same logical operation (perhaps by passing the data-context in as an argument), hut you shouldn't reuse much beyond that:

  • it caches objects; this will grow too big very quickly
  • you shouldn't share it between threads
  • once you've hit an exception, it gets very unwise to reuse

Etc. So: atomic operations fine; a long-life app context; bad.