1

I want to set up the execution strategy for Oracle in Entity Framework Core 2.2 by using the DbContext injection. I am using NuGet package Oracle.EntityFrameworkCore for connecting to an Oracle DB.

services.AddDbContext<MyContext>(options =>
        {
            options.UseOracle(configuration.GetConnectionString("MyConnection"), oracleOptionsAction: oracleOptions =>
            {
                oracleOptions.ExecutionStrategy(x => 
                    new MyExecutionStrategy();
            });
        });

I have created the following execution strategy class for testing purposes. It turns out the class DbExecutionStrategy does not exist in dotnet core:

public class MyExecutionStrategy : DbExecutionStrategy
{
    public MyExecutionStrategy() : base(10, TimeSpan.FromSeconds(30))
    {

    }

    protected override bool ShouldRetryOn(Exception exception)
    {
        return true;
    }
}

2 Answers2

4

The class DbExecutionStrategy does not exist in dotnet core only in dotnet. You must implement the abstract class ExecutionStrategy instead. The custom execution strategy class must be defined in this way:

public class MyExecutionStrategy : ExecutionStrategy
{
    public MyExecutionStrategy(ExecutionStrategyDependencies dependencies, int maxRetryCount, TimeSpan maxRetryDelay) 
        : base(dependencies, maxRetryCount, maxRetryDelay)
    {

    }

    protected override bool ShouldRetryOn(Exception exception)
    {        
        return true;
    }
}

Then the dependency injection section:

services.AddDbContext(options =>
{
    options.UseOracle(configuration.GetConnectionString("MyConnection"), oracleOptionsAction: oracleOptions =>
    {
        oracleOptions.ExecutionStrategy(dependencies => 
            new MyExecutionStrategy(
            dependencies,
            retryCount,
            retryDelay));
    });
});
2

For Oraсle it is preferable to use the OracleRetryingExecutionStrategy class of the Oracle.EntityFrameworkCore namespace. It already inherits from ExecutionStrategy and has an additional constructor parameter to which you can pass a collection of Oracle error codes to retry.

For example, here is an implementation of some class with default retry options and some Oracle code added for retry:

public class SomeRetryStrategy : OracleRetryingExecutionStrategy
{
    private static readonly IList<int> ErrorNumbersToRetry = new List<int>()
    {
        28,    // ORA-00028: Your session has been killed
        12570  // ORA-12570: Network Session: Unexpected packet read error
    };

    public SomeRetryStrategy(ExecutionStrategyDependencies dependencies)
        : base(dependencies, DefaultMaxRetryCount, DefaultMaxDelay, ErrorNumbersToRetry)
    {
    }
}
Alieh S
  • 170
  • 2
  • 8