Using Entity Framework with SQL Azure
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?
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.
If you use Entity Framework 6 (currently in alpha) then there is some new in-built support for transient retries with Azure SQL Database (with a little bit of configuration): http://entityframework.codeplex.com/wikipage?title=Connection%20Resiliency%20Spec
I've created a library which allows you to configure Entity Framework to retry using the Fault Handling block without needing to change every database call - generally you will only need to change your config file and possibly one or two lines of code.
This allows you to use it for Entity Framework < v6 or Linq To Sql.
https://github.com/robdmoore/ReliableDbProvider
链接地址: http://www.djcxy.com/p/90224.html上一篇: WCF RIA服务和SQL Azure瞬态错误处理
下一篇: 在SQL Azure中使用实体框架