问题
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.
回答1:
When connecting to 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 way you connection was dropped. Enabling a Execution Strategy as suggested by Anbuj should solve the issue.
回答2:
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);
}
回答3:
Enable an execution strategy as mentioned here : https://msdn.microsoft.com/en-us/data/dn456835.aspx . When designing for Azure SQL DB, you have to design for transient connection failures, since back-end updates, hardware failures, load balancing can cause intermittent failures at times.
回答4:
I get this error when the login I am trying to connect to the database with does not have an associated user in the database.
回答5:
This could be because of TLS setting, .net 4.5 framework don't support tls 1.2 by default and new SQL db is not compatible with older tls ver . so either disable tls 1.0,1.1 in your machine or update to .net 4.6.2
回答6:
I am posting this answer as I face lot of issue while researching the answer of the issue. Below is the detailed error message I was getting:
Dividing the errors in the parts as the error was too long:
System.Data.Entity.Core.EntityException: *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. * --->
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Resource ID : 1. The request limit for the database is 30 and has been reached. See 'http://go.microsoft.com/fwlink/?LinkId=267637' for assistance. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
After research I found that it was related to the limits of Azure SQL Database maximum logins. I was using 'Basic' service tire and max concurrent users can login with that is 30.
Azure has pricing tiers that have quite dramatic differences in performance. To achieve that, they throttle a lot of performance metrics, e.g. CPU power, requests per minute, etc.
This means that if you're pushing over your tier, your requests will start getting queued up as the CPU power / volume of requests is too high to process. This results in timeouts and then the request limit grows as requests wait to be processed. Eventually, it gets to the point where the database essentially goes down.
My experience is that the lower database levels, such as S0 and S1, are under-powered and shouldn't be used for anything other than development or very basic sites.
There are some great tools in the Azure portal that allow you to debug what is going on with your database, such as the CPU graphs, index advisor and query performance insights.
Here are the related links:
https://blogs.technet.microsoft.com/latam/2015/06/01/how-to-deal-with-the-limits-of-azure-sql-database-maximum-logins/
https://docs.microsoft.com/en-us/azure/sql-database/sql-database-dtu-resource-limits-single-databases
Conclusion:
Part 1:Enable an execution strategy as mentioned here: https://msdn.microsoft.com/en-us/data/dn456835.aspx .
Part 2: You need to upgrade the subscription in the Azure(if price permits).
Thanks.
回答7:
If your database is local to let's say a WebAPI, sometimes you must provide data source=localhost not an IP address. We have the situation where we are using some VPS and without setting data source to a localhost gives this error. So, if anybody else has experienced this, it could help him.
回答8:
The solution for this problem is through the suggested answer, use SetExecutionStrategy()
to turn on a retry policy. Also make sure to derive from the class DbConfiguration
so that Entity Framework can execute the method automatically.
You also want to make sure that your connection resiliency really works through setting up an command interception which creates connection errors so that you can confirm it works.
More about how to handle transient connection errors
来源:https://stackoverflow.com/questions/29840282/error-when-connect-database-continuously