I encapsulate my linq in sql calls in the repository class that is created in the constructor of my overloaded controller. The constructor of my repository class creates a data context, so only one data context is used for the life of the page loading.
In my repository class destructor, I explicitly call the DataContext utility, although I don't think it is necessary.
Using the performance monitor, if I monitor the count of user connections and repeatedly load the page, the number increases once per page load. Connections do not close or are not used (about 20 minutes).
I tried to include Pooling = false in my configuration to make sure that this has some effect, but it is not. In any case, with the pool, I would not expect a new connection for each load, I would expect it to reuse the connections.
I tried to set a breakpoint in the destructor to make sure that it was being cleaned, and that is accurate enough. So what is going on?
Some code to illustrate the above:
Controller:
public class MyController : Controller
{
protected MyRepository rep;
public MyController ()
{
rep = new MyRepository();
}
}
Repository:
public class MyRepository
{
protected MyDataContext dc;
public MyRepository()
{
dc = getDC();
}
~MyRepository()
{
if (dc != null)
{
dc.Dispose();
}
}
}
Note. I am adding a few tips and contextual information to DC for audit purposes. For this reason, I want one connection per page to load
Update:
IDisposable Dispose , MvcHandler. , . , , , , - MSDN, :
, MvcHandler , IDisposable, , Dispose .
:
, , MS- "using" DC . , . linq sql, DC.