2

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?

2
  • 1
    Are you sure? Both the using and Fill() should be closing it. You're probably looking at connections in the Pool, nothing to do with this code. Commented Jun 30, 2010 at 9:22
  • 1
    You can tell when filling the next Table does not add an extra connection. Commented Jun 30, 2010 at 9:32

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I would assume that it's hanging in the connectionpool

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.