Why are my connections not closed even if I explicitly manage the DataContext?

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)
        {
            //if (dc.Connection.State != System.Data.ConnectionState.Closed)
            //{
            //    dc.Connection.Close();
            //}
            dc.Dispose();
        }
    }

    // etc
}

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.

+5
3

(, ) :

public class MyRepository : IDisposable
{
    ...  // everything except the dtor

    public void Dispose()
    {
        if (dc != null)
        {
            dc.Dispose();
        } 
    }
}

public class MyController : Controller, IDisposable
{
    protected MyRepository rep;

    public MyController ()
    {
        rep = new MyRepository();
    }

    public void Dispose()
    {
       if (rep!= null)
       {
          rep.Dispose();
       } 
    }
}

() MyController use:

using (var ctl = new MyController ())
{
   // use ctl
}

Edit:
, MyController, . , .

2:
( /):

var ctl = GetController ();
using (ctl)
{
   // use ctl
}

, , ctl.Dispose() .

+4

GC. MyRepository Dispose dc.

. . # Finalize ?

MyRepository IDisposable, , .

, ,

.

using(var dc = getDC())
{
    //do stuff with the dc
}//the dc will be Disposed here

: # http://msdn.microsoft.com/en-us/library/66x5fx1b(v=VS.100).aspx

+2

, , , , .

The answer to your question is that calling Dispose in MyRepository (assuming its DataContext) does not close the connection. It just returns a connection to the pool for next use.

This SO Post , explains when you should worry about closing connections ...

0
source

All Articles