Search code examples
c#sql-serversqlexception

Simple update command causes SqlException Execution Timeout Expired


I have an application which converts a mssql-db (source) to another mssql-db (target). For every table in my source i basically get all rows and make an insert for every row to my target. I run a simple query which updates the source-row to store the target-table and target-id of the records which get inserted to the target-db:

var commandText = $"UPDATE SOURCE_TABLE SET SOURCE_TABLE.TARGET_ID = '{target_id}', SOURCE_TABLE.TARGET_TBL = '{target_table}' WHERE SOURCE_TABLE.ID = '{id}'";

base.Source.ExecuteNonQuery(commandText);

This runs perfectly fine until i try to update the source-row of a specific table. I get an SqlException Execution Timeout Expired.

I have checked the variables target_id, target_table and id they are all filled with valid values. I have disabled all triggers and the id-field is primary key.

When i run this command via management-studio, there is not problem. Doesnt even take a second to process.


I have used the activity-monitor from management-studio to monitor when the sql runs. The databases with the red box are the target-db. activity-monitor Every single peak in the second diagramm ("Wartende Tasks" = "waiting tasks"). Stands for one time the query has been tried to execute.


This is how the command is executed:

public int ExecuteNonQuery(string commandText)
{
    using (var connection = this.CreateConnection()) // new SqlConnection(CONNECTION_STRING)
    {
        connection.Open();
        using (var command = connection.CreateCommand())
        {
            command.CommandText = commandText;

            return command.ExecuteNonQuery();
        }
    }
}

Solution

  • The problem was, the process which makes the update on the source is blocked by the process which makes select on the source. I had to wrap these into an transaction to use the IsolationLevel.ReadUncommitted. Now it works.