Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to force a Dispose after a LINQ query?

My DBA says that there are way too many connection open and he thinks it is my code in .net that is leaving them open.

I am using LINQ querys and EF code first.

Example Method:

 public List<Stuff> GetStuff()
 {
      var db = new DBContext();

      var results =  db.stuff.toList();

      return results;
 }

Do I need to dispose the db var once I am done? My understanding is that I didn't need to in EF and LINQ. Please point me to a Microsoft documentation about managing connection in code or best practices for LINQ/EF and db connections

Update:

I added

db.Connection.Close();
db.Dispose();

and I still see the open connection in SQL after the two lines were executed. Is there a reason why it wouldn't close when I force it to close?

like image 874
Arcadian Avatar asked Feb 19 '15 03:02

Arcadian


People also ask

Should I dispose Datacontext?

Don't dispose DbContext objects. Although the DbContext implements IDisposable , you shouldn't manually dispose it, nor should you wrap it in a using statement.

Does LINQ query support immediate execution?

LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution. You can also force a query to execute immediately, which is useful for caching query results.

Is LINQ to SQL obsolete?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

Does LINQ select return new object?

While the LINQ methods always return a new collection, they don't create a new set of objects: Both the input collection (customers, in my example) and the output collection (validCustomers, in my previous example) are just sets of pointers to the same objects.


2 Answers

You should listen to your DBA! Yes, use a using. Do not leave connections open unnecessarily. You should connect, do your business with the db, and close that connection, freeing it up for another process. This is especially true in high volume systems.

Edit. Let me further explain with my own experiences here. In low volume processing, it probably isn't an issue, but it's a bad habit not to dispose of something explicitly or not-wrap it in a using when it clearly implements IDisposable.

In high-volume situations, this is just asking for disaster. Sql server will allot so many connections per application (can be specified in the connection string). What happens is processes will spend time waiting for connections to free up if they're not promptly closed. This generally leads to timeouts or deadlocks in some situations.

Sure, you can tweak Sql server connection mgmt and such, but everytime you tweak a setting, you're making a compromise. You must consider backups running, other jobs running, etc. This is why a wise developer will listen to their DBA's warnings. It's not always all about the code...

like image 159
Didaxis Avatar answered Oct 29 '22 13:10

Didaxis


I just asked this same question over on Programmers.SE. Robert Harvey gave a great answer.

In general, you don't need to use Using statements with Entity Framework data contexts. Lazy collections is one of the reasons why.

I encourage you to read the entire answer on Programmers.SE as well as the links Robert provides in the answer.

like image 34
Sam Axe Avatar answered Oct 29 '22 14:10

Sam Axe