Skip to content

Instantly share code, notes, and snippets.

@sachinsu
Created August 27, 2020 07:20
Show Gist options
  • Save sachinsu/d3888ab4418b937a650e880ec4682653 to your computer and use it in GitHub Desktop.
Save sachinsu/d3888ab4418b937a650e880ec4682653 to your computer and use it in GitHub Desktop.
Retrying while Opening connection to Oracle Database using Polly
using System;
using Oracle.ManagedDataAccess.Client;
using Polly;
namespace frontend.ExtensionMethods
{
public static class Extensions
{
const int retryTimes = 3;
static readonly Polly.Policy retryPolicy = Policy
//.Handle<OracleException>(e => e.Message.Contains("Connection request timed out"))
.Handle<OracleException>(ex => ex.IsRecoverable)
.WaitAndRetry(retryTimes, retryAttempt =>
TimeSpan.FromMilliseconds(Math.Pow(2, retryAttempt)), (exception, timespan, retrycount, context) =>
{
if (retrycount == 1)
{
OracleConfiguration.TraceOption = 1;
OracleConfiguration.TraceLevel = 7;
}
System.Diagnostics.Trace.WriteLine(string.Format("Retry:{0} -> failed with Error Message: {1} ", retrycount, exception.ToString()));
});
public static void OpenWithRetries(this OracleConnection conn)
{
try
{
conn.KeepAlive = true;
retryPolicy.Execute(() =>
{
conn.Open();
});
}
finally
{
OracleConfiguration.TraceLevel = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment