4

I have read both these SO questions and the MS Docs:

And have the same error. I did not have any of these in my ConnectionString: ConnectRetryCount, ConnectRetryInterval or Connection Timeout.

This is a method of my DB class:

public DataTable ExecuteSqlCommand(SqlCommand com)
{
    var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
    var retryPolicy = new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(retryStrategy);

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DataContext"].ToString());

    com.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataTable dt = new DataTable();
    try
    {
        retryPolicy.ExecuteAction(() =>
        {
            con.Open();
            da.Fill(dt);
        });
    }
    catch (Exception e)
    {
        var telemetry = new TelemetryClient(); // app insights (azure)
        telemetry.TrackException(e);
    }
    finally
    {
        con.Close();
    }
    return dt;
}

So what would be better? Remove the retry stuff from my code and use the attributes in my connection string? Let the framework do the work? Or is my current retry code sufficient? I have the feeling that the enterprise lib and retry stuff is obsolete, but cannot find a good source to confirm my thoughts.

I am using 4.7 and also have EF 6.2, but most queries are just SqlCommands using the code from above.

enter image description here

JP Hellemons
  • 5,977
  • 11
  • 63
  • 128

1 Answers1

1

Make sure the user/login you use on your connection string has access to the master database on your Azure SQL Database server also, not only to the user database. That will provide faster connections and timeouts may disappear.

Using SQL Azure Execution Estrategy may help you with this issue.

public class MyConfiguration : DbConfiguration 
{ 
    public MyConfiguration() 
    { 
        SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy()); 
    } 
}

public class MyConfiguration : DbConfiguration 
{ 
    public MyConfiguration() 
    { 
        SetExecutionStrategy( 
            "System.Data.SqlClient", 
            () => new SqlAzureExecutionStrategy(1, TimeSpan.FromSeconds(30))); 
    } 
}

For more information about SQL Azure Execution Strategy please visit this URL.

Alberto Morillo
  • 13,893
  • 2
  • 24
  • 30
  • How does that provide faster connections? And is this better then adding the retry stuff in the connectionstring? – JP Hellemons Dec 06 '17 at 11:40
  • SqlAzureExecutionStrategy retries specific errors that are known to be transient https://github.com/aspnet/EntityFramework6/pull/144/files. Timeouts could also be caused by throttling, poor schema design, lack of indexes, suboptimal query plans, blockings, timeout settings on connection strings, and more. For these type of timeouts, keep an eye on DTU consumption, # of concurrent connections, enable Query Store to optimize your queries and identify top waits. – Alberto Morillo Dec 06 '17 at 15:17
  • Some users claim semaphore errors have disappeared after switching to SqlAzureExecutionStrategy (EF6) – Alberto Morillo Dec 06 '17 at 15:35
  • 2
    But as I said, most of my code uses `SqlCommand` objects and no EF6 – JP Hellemons Dec 06 '17 at 20:06