Reputation: 101
I'm trying to implement a retry policy for database releated transient errors in Azure but don't understand how to catch errors of the type RetryLimitExceeded in this paricular context.
To elaborate - the article below on msdn describes the process of setting this up as a Entity Framework DbConfiguration which was easy enough. It however also specififies that we should handle the RetryLimitExceeded exception that might be thrown if the error is not transient related or if the number of tries exceeds the limit.
https://msdn.microsoft.com/en-us/data/dn456835.aspx
I don't understand how I should catch exceptions - should I wrap the SetExecutionStrategy in a try catch or am I expected to handle that kind of error globally? Or something else?
public class IntranetDBConfiguration : DbConfiguration
{
public IntranetDBConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy()); //default is 5 retries up to 30 seconds.
}
}
Upvotes: 0
Views: 637
Reputation: 889
Yes you have to catch
the Exception where you commit the SaveChanges()
for the context. You can try something like this.
try
{
using (MyEntities context = new MyEntities ())
{
// do your code
MyConfiguration.SuspendExecutionStrategy = true;
context.Entity.Add(entity);
context.SaveChanges();
}
}
catch (Exception ex)
{
logger.Warn("Connection error with database server.", ex);
}
finally
{
//Enable retries again...
MyConfiguration.SuspendExecutionStrategy = false;
}
Upvotes: 1