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.
- open it.
- do need do.
- 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
Post a Comment