Error: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding EnableRetryOnFailure()
Issue Description
Entity Framework Core execution gives the below error,
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseSqlServer' call.
Resolution – An exception has been raised that is likely due to a transient failure
Transient Error issues may be caused due to network connectivity issues or the temporary unavailability of a service, timeout issues, etc.
An application using shared resources in a shared environment, especially in the cloud environment is more sensitive to transient faults.
Transient faults are generally temporary in nature and often get corrected on its own provided you enable retry pattern.
To fix the issue, please enable the Retry option using any of the below methods.
Configure EnableRetry within Startup.cs as below,
Or
Please set the EnableRetry option using the OnConfiguring() method within your DBContext class as below.
Additionally, EnableRetryOnFailure() can be used for specifying the option like maximum retry count or maximum retry delay or error numbers to add, etc.
Consider enabling transient error resiliency – What is EnableRetryOnFailure()
EnableRetryOnFailure is a feature in Entity Framework Core (EF Core) used to automatically handle transient database errors during database operations, commonly used in the DbContext configuration of an EF Core application.
EnableRetryOnFailure allows build a resiliency where you can specify the number of retries and a delay between retries when an operation fails due to a transient error.
A retry features robust and resilient to database-related failures without writing custom error-handling logic.
Custom Execution strategy
Transient errors can also be fixed using the custom execution strategy.
If you wish to register a custom execution strategy then please see the below article with a sample execution strategy with custom patterns.
Database operation handling Transactions for Example- like defined with BeginTransaction() scope, etc.
Needs to be addressed differently i.e manually invoking the Execution strategy with a delegate.
Such an execution strategy should encapsulate everything that needs to be executed.
References:
For more details: Connection Resiliency for Transaction in EFCore- Guidelines and Best practices
Hope this helps you to fix the issue. Did I miss anything else in these resolution steps?
Did the above steps resolve your issue? Please sound off your comments below!
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
it’s a great solution however has an inconvenient flavor; this configuration is not compatible with transactions.
you need to manually invoke the exection strategy
more details here
https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
well if your app has many transactions around the different parts
you need to change manually all the transactions.
Hey Leo, thanks for your feedback and inputs! Yes, I agree with you. If you have a transaction involved in the operation then the default strategy will not work effectively. You should use an execution strategy using the delegate.
Hi, i tried utilizing one of your methods above,but now i am getting evaluation timed out. Here is my method
public static void ConfigureMySqlContext(this IServiceCollection services, IConfiguration config)
{
var connectionString = config["mysqlconnection:connectionString"];
//services.AddDbContext(o => o.UseMySql(connectionString));
services.AddDbContext(options =>
{
options.UseMySql(connectionString,
mySqlOptionsAction: MySqlOptions =>
{
MySqlOptions.EnableRetryOnFailure(
maxRetryCount: 10,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
});
});
}
Hi Kelvin- Thanks for your query. There could be multiple reasons for this. If possible try to use the latest MySQL NuGet packages(Old few version had time out issue) and please re-verify connection string supplied.