Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Entity Framework with SQL Azure - Reliability [closed]

I'm writing an application for Windows Azure. I'm using Entity Framework to access SQL Azure. Due to throttling and other mechanisms in SQL Azure, I need to make sure that my code performs retries if an SQL statement has failed. I'm trying to come up with a solid method to do this.

(In the code below, ObjectSet returns my EFContext.CreateObjectSet())

Let's say I have a function like this:

  public Product GetProductFromDB(int productID)
  {
     return ObjectSet.Where(item => item.Id = productID).SingleOrDefault();
  }

Now, this function performs no retries and will fail sooner or later in SQL Azure. A naive workaround would be to do something like this:

  public Product GetProductFromDB(int productID)
  {
     for (int i = 0; i < 3; i++)
     {
        try
        {
           return ObjectSet.Where(item => item.Id = productID).SingleOrDefault();
        }
        catch
        {

        }
     }
  }

Of course, this has several drawbacks. I will retry regardless of SQL failure (retry is waste of time if it's a primary key violation for instance), I will retry immediately without any pause and so on.

My next step was to start using the Transient Fault Handling library from Microsoft. It contains RetryPolicy which allows me to separate the retry logic from the actual querying code:

  public Product GetProductFromDB(int productID)
  {
     var retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(5);

     var result = _retryPolicy.ExecuteAction(() =>
        {
           return ObjectSet.Where(item => item.Id = productID).SingleOrDefault;
        });

     return result;
  }

The latest solution above is described as a http://blogs.msdn.com/b/appfabriccat/archive/2010/10/28/best-practices-for-handling-transient-conditions-in-sql-azure-client-applications.aspx Best Practices for Handling Transient Conditions in SQL Azure Client Application (Advanced Usage Patterns section).

While this is a step forward, I still have to remember to use the RetryPolicy class whenever I want to access the database via Entity Framework. In a team of several persons, this is a thing which is easy to miss. Also, the code above is a bit messy in my opinion.

What I would like is a way to enforce that retries are always used, all the time. The Transient Fault Handling library contains a class called ReliableSQLConnection but I can't find a way to use this with Entity Framework.

Any good suggestions to this issue?

like image 738
Nitramk Avatar asked May 02 '11 17:05

Nitramk


People also ask

What is the limitation of Azure SQL Database currently?

Data storage limits in Azure SQL Database have increased from 1.5 TB to 2 TB for single databases and elastic pools configured with 8 and 10 vcores. Workloads requiring up to 2 TB storage can now reduce costs by not having to increase the compute size beyond 8 vcores.

How should transient network connectivity issues in Azure SQL be handled by a client application?

Applications that connect to your database should be built to expect these transient errors. To handle them, implement retry logic in their code instead of surfacing them to users as application errors. If your client program uses ADO.NET, your program is told about the transient error by the throw of SqlException.

Can't connect to Azure SQL managed instance?

If you are unable to connect to SQL Managed Instance from an Azure virtual machine within the same virtual network but a different subnet, check if you have a Network Security Group set on VM subnet that might be blocking access.

What is azure entity framework?

The Entity Framework provides the glue between your object oriented code and the SQL Azure relational database in a framework that is fully compatible with your skills and development tools.


1 Answers

After the above remarks, Microsoft created the transient fault handling library, which "includes direct support for working with SQL Azure through the ReliableSqlConnection class."

http://msdn.microsoft.com/en-us/library/hh680899(v=pandp.50).aspx

Most developers looking to use Azure will find the above to be of great help.

like image 180
user1255756 Avatar answered Sep 22 '22 02:09

user1255756