连接到 Oracle DB 时异步等待 Polly 代码抛出错误
async await throwing error for Polly code while connecting to Oracle DB
下面是我尝试使用 Polly 与 Oracle 数据库保持一致连接的代码。
public async Task<bool> Execute()
{
var retryTimes = 3000;
var waitBetweenExceptions = TimeSpan.FromSeconds(10);
var retryPolicy = await Policy
.Handle<OracleException>(e => e.Message.Contains("ORA-12543") || e.Message.Contains("ORA-12170"))
.WaitAndRetry(retryTimes, i => waitBetweenExceptions);
retryPolicy.Execute(() =>
{
string cs = "Data Source=SampleDB;User Id=userid;Password=password;Connection Timeout=600; Max Pool Size=150;";
using (OracleConnection connection = new OracleConnection(cs))
{
for (int i = 0; i < 1000; i++)
{
connection.Open();
Console.WriteLine("Counter :" + i);
Thread.Sleep(1000);
connection.Close();
}
}
});
return true;
}
我收到 Cannot await void
编译错误。另外,我想尝试 Polly 的 WaitForever 方法。
我尝试了类似下面的问题,但对如何成功使用它有点困惑。
编辑
同步方式修改问题
我尝试了下面的代码,但仍然无法正常工作...
class Program
{
static void Main(string[] args)
{
Connect connect = new Connect();
connect.Execute();
Console.ReadKey();
}
}
public class Connect
{
public void Execute()
{
var retryTimes = 3000;
var retryableErrorCodes = new[] { "ORA-12543", "ORA-12170" };
RetryPolicy retryPolicy = Policy
.Handle<OracleException>(ex => retryableErrorCodes.Any(errorCode => ex.Message.Contains(errorCode)))
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(10));
retryPolicy.Execute(() =>
{
string cs = "Data Source=SampleDB;User Id=userid;Password=password;Connection Timeout=600; Max Pool Size=150;";
using (OracleConnection connection = new OracleConnection(cs))
{
for (int i = 0; i < 1000; i++)
{
connection.Open();
Console.WriteLine("Counter :" + i);
Thread.Sleep(1000);
connection.Close();
}
}
});
}
}
我试过使用同步方式。但是,断开网线后,它只是静静地停止而不是自动重新连接。
修饰码为sync
或async
。
同步案例
政策声明
var retryableErrorCodes = new[] { "ORA-12543", "ORA-12170" };
RetryPolicy retryPolicy = Policy
.Handle<OracleException>(ex => retryableErrorCodes.Any(errorCode => ex.Message.Contains(errorCode)))
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(10));
政策使用
retryPolicy.Execute(() =>
{
//...
});
异步大小写
政策声明
var retryableErrorCodes = new[] { "ORA-12543", "ORA-12170" };
AsyncRetryPolicy retryPolicy = Policy
.Handle<OracleException>(ex => retryableErrorCodes.Any(errorCode => ex.Message.Contains(errorCode)))
.WaitAndRetryAsync(retryTimes, _ => TimeSpan.FromSeconds(10));
政策使用
await retryPolicy.ExecuteAsync((ct) =>
{
//...
}, CancellationToken.None);
WaitAndRetryForever
和 WaitAndRetryForeverAsync
是相同的,但您不必在那里指定最大重试次数。
下面是我尝试使用 Polly 与 Oracle 数据库保持一致连接的代码。
public async Task<bool> Execute()
{
var retryTimes = 3000;
var waitBetweenExceptions = TimeSpan.FromSeconds(10);
var retryPolicy = await Policy
.Handle<OracleException>(e => e.Message.Contains("ORA-12543") || e.Message.Contains("ORA-12170"))
.WaitAndRetry(retryTimes, i => waitBetweenExceptions);
retryPolicy.Execute(() =>
{
string cs = "Data Source=SampleDB;User Id=userid;Password=password;Connection Timeout=600; Max Pool Size=150;";
using (OracleConnection connection = new OracleConnection(cs))
{
for (int i = 0; i < 1000; i++)
{
connection.Open();
Console.WriteLine("Counter :" + i);
Thread.Sleep(1000);
connection.Close();
}
}
});
return true;
}
我收到 Cannot await void
编译错误。另外,我想尝试 Polly 的 WaitForever 方法。
我尝试了类似下面的问题,但对如何成功使用它有点困惑。
编辑 同步方式修改问题
我尝试了下面的代码,但仍然无法正常工作...
class Program
{
static void Main(string[] args)
{
Connect connect = new Connect();
connect.Execute();
Console.ReadKey();
}
}
public class Connect
{
public void Execute()
{
var retryTimes = 3000;
var retryableErrorCodes = new[] { "ORA-12543", "ORA-12170" };
RetryPolicy retryPolicy = Policy
.Handle<OracleException>(ex => retryableErrorCodes.Any(errorCode => ex.Message.Contains(errorCode)))
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(10));
retryPolicy.Execute(() =>
{
string cs = "Data Source=SampleDB;User Id=userid;Password=password;Connection Timeout=600; Max Pool Size=150;";
using (OracleConnection connection = new OracleConnection(cs))
{
for (int i = 0; i < 1000; i++)
{
connection.Open();
Console.WriteLine("Counter :" + i);
Thread.Sleep(1000);
connection.Close();
}
}
});
}
}
我试过使用同步方式。但是,断开网线后,它只是静静地停止而不是自动重新连接。
修饰码为sync
或async
。
同步案例
政策声明
var retryableErrorCodes = new[] { "ORA-12543", "ORA-12170" };
RetryPolicy retryPolicy = Policy
.Handle<OracleException>(ex => retryableErrorCodes.Any(errorCode => ex.Message.Contains(errorCode)))
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(10));
政策使用
retryPolicy.Execute(() =>
{
//...
});
异步大小写
政策声明
var retryableErrorCodes = new[] { "ORA-12543", "ORA-12170" };
AsyncRetryPolicy retryPolicy = Policy
.Handle<OracleException>(ex => retryableErrorCodes.Any(errorCode => ex.Message.Contains(errorCode)))
.WaitAndRetryAsync(retryTimes, _ => TimeSpan.FromSeconds(10));
政策使用
await retryPolicy.ExecuteAsync((ct) =>
{
//...
}, CancellationToken.None);
WaitAndRetryForever
和 WaitAndRetryForeverAsync
是相同的,但您不必在那里指定最大重试次数。