In MS Test - running parallel tests - sql timeout comes

Palak Shah

Well-known member
Joined
Apr 29, 2020
Messages
97
Programming Experience
1-3
I have framework which runs 12 threads parallel - each will have different test but may have to get result from sql connection

Doing so - I'm receiving below error - Is there a way where I can do any workaround in my code to make it work without loosing parallel threads

System.Data.SqlClient.SqlException: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding..

My Config:
<add name="ConnectionString1" connectionString="data source=54.154.178.120;initial catalog=test;persist security info=true;user id=admin;password=admin" providerName="System.Data.SqlClient" />

Code:
private static void ExecuteQuery(TestConfigurationCDO testConfiguration, string query)
{
    using (var sqlConnection = new SqlConnection(testConfiguration.ConnectionString))
    {
        var orderTable = sqlConnection.ExecuteQuery(query);
    }
}


//Execution
public static DataTable ExecuteQuery(this SqlConnection sqlConnection, string queryString)
{
    if (sqlConnection == null) throw new ArgumentNullException(nameof(sqlConnection));

    DataSet dataset;
    try
    {
        //Checking the state of the connection
        if (sqlConnection.State == ConnectionState.Closed ||
            sqlConnection.State == ConnectionState.Broken)
            sqlConnection.Open();

        SqlDataAdapter dataAdaptor = new SqlDataAdapter
        {
            SelectCommand = new SqlCommand(queryString, sqlConnection) { CommandType = CommandType.Text }
        };

        dataset = new DataSet();
        dataAdaptor.Fill(dataset, "table");
        sqlConnection.Close();
        return dataset.Tables["table"];
    }
    catch (Exception e)
    {
        dataset = null;
        sqlConnection.Close();
        Console.WriteLine("ERROR {0}", e);
        //LogHelpers.Write("ERROR :: " + e.Message);
        return null;
    }
    finally
    {
        sqlConnection.Close();
        dataset = null;
    }
}
 
Last edited by a moderator:
If the query takes a long time, then the query takes a long time. There's not much you can do in the C# code, other than extend the default CommandTimeOut value.

The better approach is to make the query not take a long time. Rewrite the SQL and/or use indexes.
 
Back
Top Bottom