Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when connect database continuously

When I am querying from database in continuous looping, after some time I get an error :

An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy.

Normally it is working fine.

like image 461
user3928324 Avatar asked Apr 24 '15 06:04

user3928324


People also ask

What is SQL transient error?

A transient error, also known as a transient fault, has an underlying cause that soon resolves itself. An occasional cause of transient errors is when the Azure system quickly shifts hardware resources to better load-balance various workloads. Most of these reconfiguration events finish in less than 60 seconds.

What is meant by transient error?

Transient errors are errors which are recoverable: a connection was temporarily not available, timeouts etc.. Some strategies check for transient errors, and if such an error occurs, they will retry, and otherwise fail. Other strategies will always retry, no matter what the error is.

Can't connect to Azure SQL Database?

If the application persistently fails to connect to Azure SQL Database, it usually indicates an issue with one of the following: Firewall configuration. The Azure SQL database or client-side firewall is blocking connections to Azure SQL Database.

How does Entity Framework handle transient failure?

The solution is to manually invoke the execution strategy with a delegate representing everything that needs to be executed. If a transient failure occurs, the execution strategy will invoke the delegate again. using var db = new BloggingContext(); var strategy = db. Database.


2 Answers

If you are using EF Core configure retry on failure for resilient connections :

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {     optionsBuilder.UseSqlServer("your_connection_string", builder =>         {             builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);         });     base.OnConfiguring(optionsBuilder); } 
like image 197
Shashank Shekhar Avatar answered Sep 22 '22 08:09

Shashank Shekhar


When connecting to a SQL Database you have to account for transient connection failures. These connection failures can happen for example when updates are rolled out, hardware fails etc. The error you see indicates that one of these things happened which is why your connection was dropped. Enabling an Execution Strategy as suggested by Anbuj should solve the issue.

like image 31
Jan Engelsberg Avatar answered Sep 21 '22 08:09

Jan Engelsberg