“信号量超时时间已过期” SQL Azure [英] "The semaphore timeout period has expired" SQL Azure

查看:171
本文介绍了“信号量超时时间已过期” SQL Azure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个.Net MVC Azure网站,其中包含使用Entity Framework 6访问的SQL Azure数据库。间歇性地(一千个请求中的一个),我收到错误System.ComponentModel.Win32Exception:信号量超时期限已过期


System.Data.SqlClient.SqlException:从服务器接收结果时,传输级错误发生
。 (提供程序:TCP
提供程序,错误:0 - 信号量超时时间已过期。)--->
System.ComponentModel.Win32Exception:信号量超时期限为
过期


似乎没有理由和错误之前和之后的请求以及与SQL Azure的交互都很好。有没有办法处理或解决这个问题。

解决方案

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)

这篇关于“信号量超时时间已过期” SQL Azure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆