title: Implementing connection retry policy on failure to connect with database
body: <p>I have my database on cloud i.e Azure so sometimes I get network related error like this:</p>
  
  <blockquote>
    <p>A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)</p>
  </blockquote>
  
  <p>I have decide to use <a href="https://github.com/App-vNext/Polly" rel="nofollow noreferrer">Polly</a> to retry connection attempt after some time, but I am not sure whether I have used it in the right way or not :</p>
  
  <pre><code>public class AddOperation
  {
      public void Start()
      {
            using (var processor = new MyProcessor())
            {
                for (int i = 0; i &lt; 2; i++)
                {
                    if(i==0)
                    {
                       var connection = new SqlConnection("Connection string 1");
                       processor.Process(connection);
                    }
                    else
                    {
                        var connection = new SqlConnection("Connection string 2");
                        processor.Process(connection);
                    }   
                }
            }
      }       
  }
  
  public class MyProcessor : IDisposable
  {
      public void Process(DbConnection cn)
          {
              using (var cmd = cn.CreateCommand())
              {
                  cmd.CommandText = "query";
                  cmd.CommandTimeout = 1800;
                  RetryPolicy retryPolicy = Policy
                        .Handle&lt;DbException&gt;()
                        .WaitAndRetry(new[]
                        {
                          TimeSpan.FromSeconds(3),
                          TimeSpan.FromSeconds(6),
                          TimeSpan.FromSeconds(9)
                        });
                  retryPolicy.Execute(() =&gt; ConnectionManager.OpenConnection(cn));
                  using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                  { 
                     //code
                  }
              }
          }
  }
  
  public class ConnectionManager
  {
          public static void OpenConnection(DbConnection cn)
          {
              try
              {
                  cn.Open();
                  return;
              }
              catch(DbException ex)
              {
                  throw ex;
              }
          }
      }
  </code></pre>
  
  <p>As per my understanding Polly will work something like this :</p>
  
  <p><strong>1st attempt</strong> : Wait 3 seconds then call <code>ConnectionManager.OpenConnection(cn)</code> again</p>
  
  <p><strong>2nd attempt</strong> : Wait 6 seconds then call <code>ConnectionManager.OpenConnection(cn)</code> again on <code>DbException</code></p>
  
  <p><strong>3rd attempt :</strong> Wait 9 seconds then call <code>ConnectionManager.OpenConnection(cn)</code> again on <code>DbException</code></p>
  
  <p>But what if <code>DbException</code> occurs again? Will it process or send to my catch clause wrapping up Process method?</p>
  
  <p>I am not sure whether I have understood it properly and implemented it correctly.</p>
  
  <p>I will appreciate any help :)</p>