c# - Am I using DataContext.Dispose() properly? -


i've been getting lots of connection pool timeouts in asp.net mvc project. i've been reading though linq-to-sql should disposing me doesn't work , not manually disposing inherits idisposable bad practice.

i'm using repository pattern linq-to-sql statements use. not knowing put datacontext.dispose() method, put in submitchanges() function consists of 2 lines of code:

public void submitchanges() {     db.submitchanges();     db.dispose(); } 

is place or doing wrong?

if implements idisposable easiest way is:

using(var context = new datacontext()){   blah blah } 

this ensures disposed @ appropriate time.

  1. open it.
  2. do need do.
  3. close it.

this way don't have worry hanging around, , not getting called because of exception etc.

the dispose key word (msdn link).

since related, here link dispose , finalize. sounds in case, want implement repository implements idisposable. way calling object can create it, needs , close it. can clean data context when disposed/finalized.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -