G
Guest
I am developing on a website that is utilizing SQL Server 2000. Shortly
after deploying the site, we began having timeout issues due to the max
connections.
1st instinct was to diligently tidy up our connections. We did so by
encapsulating with the using statement. Ex:
using (SqlConnection myConnection = new
SqlConnectionConfigurationSettings.AppSettings["ConnectionString"]))
{
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "sp_DELETE_SOME_STUFF";
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
myCommand.ExecuteNonQuery();
}
The problem continued, obvious by monitoring in SQL Server(Management >
Current Activity > Process Info). Though my team's research indicated the
code would be handled the same, we closed and disposed the connection in the
finally block. Ex:
try
{
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "sp_DELETE_SOMESTUFF";
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
myConnection.Dispose();
myConnection = null;
}
To our amazement this worked and our users on the database have decreased to
the level of our expectation.
All I have read say these two methods are equals; What is the logic that
should be followed as to when to implement each technique? Are there
variables with in our server/database envronments that could cause this
difference?
Many thanks!
after deploying the site, we began having timeout issues due to the max
connections.
1st instinct was to diligently tidy up our connections. We did so by
encapsulating with the using statement. Ex:
using (SqlConnection myConnection = new
SqlConnectionConfigurationSettings.AppSettings["ConnectionString"]))
{
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "sp_DELETE_SOME_STUFF";
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
myCommand.ExecuteNonQuery();
}
The problem continued, obvious by monitoring in SQL Server(Management >
Current Activity > Process Info). Though my team's research indicated the
code would be handled the same, we closed and disposed the connection in the
finally block. Ex:
try
{
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "sp_DELETE_SOMESTUFF";
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
myConnection.Dispose();
myConnection = null;
}
To our amazement this worked and our users on the database have decreased to
the level of our expectation.
All I have read say these two methods are equals; What is the logic that
should be followed as to when to implement each technique? Are there
variables with in our server/database envronments that could cause this
difference?
Many thanks!