KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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.msd
Tags (comma-separated)
Save Edits
Cancel