且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

“信号量超时时间已过期” SQL Azure

更新时间:2023-02-26 15:58:53

Azure SQL与前提SQL非常不同。当Azure SQL Server重载或关闭时,它将断开连接的数目,当您重新连接时,您将被发送到另一个SQL Server。



但是使用TCP连接你不知道另一端是否终止了连接,除非你真的发送信息,这就是为什么会出现这个错误。



一旦你的代码知道连接是终止,它建立一个新的连接在下一个查询,这将工作正常。



使用Entity Framework 6,您现在可以处理使用实体框架对SQL Azure进行故障处理



在您的DBConfiguration类中,您需要设置SetExecutionStrategy并对其进行配置。只需在项目中创建一个新类并继承自DbConfiguration。

  public class MyConfiguration:DbConfiguration 
{
public MyConfiguration()
{
SetExecutionStrategy(
System.Data.SqlClient,
()=> new SqlAzureExecutionStrategy(1,TimeSpan.FromSeconds(30))) ;
}
}

连接弹性/重试逻辑(EF6以上)


I am running a .Net MVC Azure Web Site with a SQL Azure database accessed using Entity Framework 6. Intermittently (1 in a thousand or so requests), I get the error "System.ComponentModel.Win32Exception: The semaphore timeout period has expired"

System.Data.SqlClient.SqlException: A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The semaphore timeout period has expired.) ---> System.ComponentModel.Win32Exception: The semaphore timeout period has expired

There seems to be no reason for it and requests before and after the error and their interactions with SQL Azure are fine. Is there any way to handle or resolve this.

Azure SQL is very different than on premise SQL. When an Azure SQL Server gets overloaded or goes down, it will disconnect a number of connections and when you reconnect you will get sent to another SQL Server.

However with TCP connections you don't know if the other end has terminated the connection unless you actually send information down it, which is why this error occurs.

Once your code know the connection is terminated, it establishes a new connection on the next query, which will work just fine.

With Entity Framework 6 you can now deal with Transient Fault Handling with SQL Azure using Entity Framework

In your DBConfiguration class you need to set your SetExecutionStrategy and configure it. Just create a new class in your project and inherit from DbConfiguration.

public class MyConfiguration : DbConfiguration 
{ 
    public MyConfiguration() 
    { 
        SetExecutionStrategy( 
            "System.Data.SqlClient", 
            () => new SqlAzureExecutionStrategy(1, TimeSpan.FromSeconds(30))); 
    } 
}

Full details at Connection Resiliency / Retry Logic (EF6 onwards)