I asked this before but got side tracked and found a work around but not an answer. This was my original post:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22587075.htmlHere is my original question:
Is there a way to see how many SqlConnection's I have open (that I didn't close) while debugging my app? So by the time my app is done I want to check how many connections I still have open that I need to close. Can I use a trace or do I have to code something?
The problem Im having has to do with Connection Pooling issues. I dont think the connection is closed when I leave this function (below). Im not trusting CommandBehavior.CloseConn
ection. My mistrust is based on these links:
http://codebetter.com/blogs/sahil.malik/archive/2005/01/16/44678.aspxhttp://codebetter.com/blogs/sahil.malik/archive/2005/03/31/60950.aspxhttp://forums.mysql.com/read.php?38,63733,63733// The Function in Question:
public SqlDataReader GetRatesByPAGE (string PAGE, string vDataSource) {
SqlConnection cnn = new SqlConnection(GetConnectio
nString(vD
ataSource)
);
SqlCommand cmd = new SqlCommand("Refacts_GetBas
eRatesPage
", cnn);
cmd.CommandType = CommandType.StoredProcedur
e;
SqlParameter parmPAGE = new SqlParameter("@Page", SqlDbType.VarChar, 7);
parmPAGE.Value = PAGE;
cmd.Parameters.Add(parmPAG
E);
cnn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandB
ehavior.Cl
oseConnect
ion);
return reader;
}
Here is something I found that tracks the connections opening and closing. I implement by placing the handler event in a class that has the function (such as above) and in the function, after the cnn is created, I place the handler. But I'm not convinced it actually is doing what I think I want which is to accurately tell me if the cnn is still open or closed. It says it is but can I trust that?
// This is my reference
//
http://msdn2.microsoft.com/en-us/library/a0hee08w(VS.71).aspx// This is adding the handler
cnn.StateChange += new StateChangeEventHandler(On
StateChang
e);
// This is the handler event
protected static void OnStateChange(object sender, StateChangeEventArgs args) {
StreamWriter sWriter=new StreamWriter(@"C:\Inetpub\
wwwroot\St
age3_Devel
opment\XML
_WebServic
e\bin\OnSt
ateChange.
txt", true);
sWriter.WriteLine("The current Connection state has changed from {0} to {1}.", args.OriginalState, args.CurrentState);
sWriter.Flush();
sWriter.Close();
}
I have my suspicions because I know if I try to close or dispose of my cnn before I return the reader then I get this error from the caller.
"Invalid attempt to HasRows when reader is closed."
That tells me that the reader cant exist without a connection. I know that CommandBehavior.CloseConn
ection will close the connection after the reader is closed but Im not convinced. Is the above handler just reacting to the CommandBehavior.CloseConn
ection but not really realizing that the cnn is still open?
Does anyone have any intimate knowledge/experience with this?