Implementing Retry Policy using Data Access Application Block (Enterprise Library)

Answered Implementing Retry Policy using Data Access Application Block (Enterprise Library)

  • Thursday, October 07, 2010 10:28 AM
     
     

    I have implemented the connectivity to SQL Azure using Enterprise Library 5.0. Which would be the best approach to implement the Retry Policy in to this implementation?

     

    Thanks in Advance

    Sreejith

     

All Replies

  • Friday, October 08, 2010 9:41 AM
    Moderator
     
     Answered

    Hi,

    The usage of Enterprise Library is very like the usage of ADO.NET + strongly typed DataSet. To implement Retry, you need Try..Catch + a loop, just like the code mentioned below:

    http://blogs.msdn.com/b/bartr/archive/2010/06/18/sql-azure-connection-retry.aspx

    You may also put the retry logic in an extension method, which would make your code cleaner. For instance, you may write an extension method for UpdateDataSet method:

       public static class Utility
        {
            public static int UpdateDataSetWithRetry(this Database db,int retrytimes)
            {
                int returnvalue = 0;
                for (int i = 0; i < retrytimes; i++)
                {
                    try
                    {
                        //...
                        // call db.UpdateDataSet() method
                        //...
                    }
                    catch (SqlException ex)
                    {
                        //...
                    }
                    catch (Exception ex)
                    {
                        // catch other exceptions if needed
                    }
                }
                return returnvalue;
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                Database db = DatabaseFactory.CreateDatabase();
    //...

    // Retry 3 times if failed to update.
                db.UpdateDataSetWithRetry(3);
            }
        }

    Learn more about extension method:

    http://msdn.microsoft.com/en-us/library/bb383977.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Windows Azure Platform China Blog: http://blogs.msdn.com/azchina/default.aspx