D
D
Dmitry Gavrilenko2015-12-18 10:40:55
SQL
Dmitry Gavrilenko, 2015-12-18 10:40:55

Why is SQL Server not responding?

Hello. There is a small piece of code for updating a field in a table

private void SetResponsibleInSQL(long id, long resp)
    {
      using (SqlConnection con = new SqlConnection(@"Data Source=SERVER;Initial Catalog=DB;User ID=su;Password=123123;Timeout=300;"))
      {
        con.Open();
        using (SqlCommand comm = new SqlCommand()) {
          comm.Connection = con;
          comm.CommandTimeout = 300;
          comm.CommandText = String.Format("update Contractor set Responsible = {0} where Id = {1}", resp, id);
          comm.ExecuteNonQuery();
          comm.Dispose();
          comm.Cancel();
        }
        con.Close();
      }
    }

This method is executed in a loop. Each iteration, without calling this function, takes about 3 seconds. This is not the only database request in the iteration, but this one is timed out. Then 8, then 10, then 3 iterations out of 25 possible passes. I even increased the timeouts by 300, but it still doesn't help.
Error:
[System.Data.SqlClient.SqlException: Timeout expired. The timeout expired before the operation was completed, or the server is not responding.
This statement was aborted.]
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String timeName), at String timeName),
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
to System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

Machine running code: i3-4130 3.4GHz , 8Gb RAM
SQL server: i5-2500 3.6GHz, 12Gb RAM
UPD1: Updated the method following Dmitry Kovalsky's recommendations
_con.Open();
SetResponsibleInSQL();
_con.Close();
//---------------------------
private void SetResponsibleInSQL()
{
  using (SqlCommand comm = new SqlCommand())
  {
    comm.Connection = _con;
    comm.CommandTimeout = 30;
    comm.CommandText = "BEGIN TRANSACTION;\n" + sqlQuery + "\nCOMMIT TRANSACTION;";
    Console.WriteLine(comm.CommandText);
    comm.ExecuteNonQuery();
  }
}

Now the request is executed 1 time and it contains the Nth number of updates.
Example CommandText:
BEGIN TRANSACTION;
update C SET C.Responsible = U.Id FROM [Contractor] as C, [User] as U where C.KodV1S = '000000014' and U.UserName = 'Yangurazova' and C.IsDeleted = 0;update C SET C. Responsible = U.Id FROM [Contractor] as C, [User] as U where C.KodV1S = '000000440' and U.UserName = 'Yangurazova' and C.IsDeleted = 0;update C SET C.Responsible = U.Id FROM [Contractor] as C, [User] as U where C.KodV1S = '000002351' and U.UserName = 'Burmistrova' and C.IsDeleted = 0;update C SET C.Responsible = U.Id FROM [Contractor] as C, [User] as U where C.KodV1S = '000001702' and U.UserName = 'Pugach' and C.IsDeleted = 0;update C SET C.Responsible = U.Id FROM [Contractor] as C, [User] as U where C.KodV1S = '000002077' and U.UserName = 'Skorobogatova' and C.IsDeleted = 0;update C SET C.Responsible = U.Id FROM [Contractor] as C, [User] as U where C.KodV1S = '000001913' and U.UserName = 'Khromikhina' and C.IsDeleted = 0;
COMMIT TRANSACTION;
Especially for Dmitry, I summarize: even such actions do not help solve the problem with TimeOut.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2015-12-18
@dmitryKovalskiy

For cyclic execution of such code, they are punished with a workplace in the basement without a window. Dear - try to make 1 script, not hundreds. Win the darkness of time and the server will breathe a sigh of relief. Yes, the script will be more difficult and trickier, but optimizing interaction with the database is not an easy task at all, and here you create hundreds of connections / disconnections with one client, who knows what.
UPD: I read a little into the code and did not understand some of your movements.
1) why do using (SqlConnection con = new SqlConnection() and then con.Open(); and con.Close() on exit? well suppose the connection is not opened when object is created, but when leaving using and AUTOMATICALLY calling con .Dispose() will close the connection
2)
using (SqlCommand comm = new SqlCommand()) {
comm.Dispose();
comm.Cancel();
}
Firstly, Dispose is called when you go beyond the boundaries of using, and why you call Cancel I have no idea at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question