U
Usman Jamil
Hi
I've a class that creates a connection to a database, gets and loop on a
dataset given a query and then close the connection. When I use netstat
viewer to see if there is any connection open left, I always see that there
are 2 connections open and in "ESTABLISHED" state. Here is the piece of code
that I'm using, please tell where I'm doing it wrong. Since this class is
being used at many placed in my actual web based application that uses it
extensively, so the open connections grow alot, occupying most of the ports
of system and some time creating problem for other port based applications
like ftp.
Few things that I noted on debugging it line by line and monitoring from
netStats viewer.
1. On execution of m_con.Open, it shows 1 or sometimes 2 connections in
"Established" state. No idea why it shows 2 open connections, where it
should only be one.
2. If I only see one connection in established state after m_con.Open
statement, then the second connection in established state comes when i'm
looping over the dataset records. So at the end there are two connections
left open no matter what.
3. Disposing dataset, sqlcommand, sqladapter, or sqlconnection does'nt have
any effect on the state of the connections that are opened.
4. Somewhere on the net I read that the opened connections are not disposed
rather returned to a pool but in a non-established state to improve
performance, but the connection my application opens stay in "established"
state.
5. When I close my application, all the open and established connections
(two in my case) are deleted from the Netstat viewer utility, i.e. the
opened db sessions are closed on application exit.
Please check it and tell me where I'm making the mistake or how to release
the openend connection.
/////////////////////////////////////////////////////CODE//////////////////////////////////////////////////////////
string strSql="select * from mytable";
#region Open connection
SqlConnection m_con = null;
m_con = new
SqlConnection("server=MyServer;database=MyDB;uid=sa;pwd=sa;");
m_con.Open();
#endregion
#region Create command
SqlCommand cmd = new SqlCommand(strSql,m_con);
cmd.CommandTimeout = 1000;
cmd.CommandType = CommandType.Text;
#endregion
#region Fill data set
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
#endregion
#region Loop over dataset
if(dataSet.Tables[0].Rows.Count > 0)
{
foreach (DataRow reader in dataSet.Tables[0].Rows)
{
string szField1 = reader["field1"].ToString();
string szField2 = reader["field2"].ToString();
}
}
#endregion
#region Dispose dataset
dataSet.Clear();
dataSet.Dispose();
#endregion
#region Dispose Adapter and command
adapter.Dispose();
cmd.Dispose();
#endregion
#region Dispose connection object
m_con.Close();
m_con.Dispose();
m_con = null;
#endregion
/////////////////////////////////////////////////////END OF
CODE//////////////////////////////////////////////////////////
I've a class that creates a connection to a database, gets and loop on a
dataset given a query and then close the connection. When I use netstat
viewer to see if there is any connection open left, I always see that there
are 2 connections open and in "ESTABLISHED" state. Here is the piece of code
that I'm using, please tell where I'm doing it wrong. Since this class is
being used at many placed in my actual web based application that uses it
extensively, so the open connections grow alot, occupying most of the ports
of system and some time creating problem for other port based applications
like ftp.
Few things that I noted on debugging it line by line and monitoring from
netStats viewer.
1. On execution of m_con.Open, it shows 1 or sometimes 2 connections in
"Established" state. No idea why it shows 2 open connections, where it
should only be one.
2. If I only see one connection in established state after m_con.Open
statement, then the second connection in established state comes when i'm
looping over the dataset records. So at the end there are two connections
left open no matter what.
3. Disposing dataset, sqlcommand, sqladapter, or sqlconnection does'nt have
any effect on the state of the connections that are opened.
4. Somewhere on the net I read that the opened connections are not disposed
rather returned to a pool but in a non-established state to improve
performance, but the connection my application opens stay in "established"
state.
5. When I close my application, all the open and established connections
(two in my case) are deleted from the Netstat viewer utility, i.e. the
opened db sessions are closed on application exit.
Please check it and tell me where I'm making the mistake or how to release
the openend connection.
/////////////////////////////////////////////////////CODE//////////////////////////////////////////////////////////
string strSql="select * from mytable";
#region Open connection
SqlConnection m_con = null;
m_con = new
SqlConnection("server=MyServer;database=MyDB;uid=sa;pwd=sa;");
m_con.Open();
#endregion
#region Create command
SqlCommand cmd = new SqlCommand(strSql,m_con);
cmd.CommandTimeout = 1000;
cmd.CommandType = CommandType.Text;
#endregion
#region Fill data set
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
#endregion
#region Loop over dataset
if(dataSet.Tables[0].Rows.Count > 0)
{
foreach (DataRow reader in dataSet.Tables[0].Rows)
{
string szField1 = reader["field1"].ToString();
string szField2 = reader["field2"].ToString();
}
}
#endregion
#region Dispose dataset
dataSet.Clear();
dataSet.Dispose();
#endregion
#region Dispose Adapter and command
adapter.Dispose();
cmd.Dispose();
#endregion
#region Dispose connection object
m_con.Close();
m_con.Dispose();
m_con = null;
#endregion
/////////////////////////////////////////////////////END OF
CODE//////////////////////////////////////////////////////////