views:

69

answers:

3

Further to my recent questions, I've now closed most of the connections that our web application was leaving open. However, the connection created by the function below remains open. Can anyone identify why it is not closing?

public DataTable GetSubDepartment(int deptId)
{   
    DataTable dt = new DataTable();
    using (SqlConnection conn = new SqlConnection(Defaults.ConnStr))
    {
        SqlCommand cmd = new SqlCommand("proc_getDepartmentChild", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@dptParent", deptId));

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        da.Fill(dt);
    }
    return dt;
}

* EDIT * Following @HenkHolterman's comment: I'm using SQL Server Management Studio Activity log to view the open connections. This one is listed as sleeping. SO what you say makes sense. Is there any way I can tell that this is a pooled connection rather than an open one?

+1  A: 

Most probably because it went back to the connection pool.

Call

SqlConnection.ClearAllPools();

to clear the pool, then it should disappear. This could sometimes be useful, but is usually not needed.

Stefan Steinegger
+1  A: 

I would assume that it's hanging in the connectionpool

cyberzed
A: 

SqlCommand and SqlDataAdapter implement IDisposable. Use the using statement for these two objects too.

Another thought: Maybe your stored procedure needs an explicit “Close”. Close the connection object at the end of the using statement . This is recommended for all open connections. Maybe it’s necessary for your stored procedure.

Dirk
wrapping each of these in their own `using` statement does not alter the status of the connection (it remains open as before).
fearoffours
Keep this anyway. It is best practice to dispose disposable objects. I have extended my answer with another thought.
Dirk
tried this as well, (with no change) thanks.
fearoffours