2016-05-26 45 views
0

我有一個Azure SQL數據庫支持我的webapp。Azure SQL超時

坐在虛擬目錄中有一個Web API 2.0。

我的網絡應用程序做一些輕量級的數據庫的東西,API負責輸入數據,而webapp部分是供用戶登錄和查看他們跟蹤的電子郵件數據。

很簡單的東西。

現在問題似乎是db。我不斷收到這些討厭的Sql Timeout錯誤。

奇怪的是,它始終是相同的錯誤。我應該提到webapi是使用NHibernate構建的。門戶網站主要是使用Code First的實體框架。

我已經聯繫了一些來自Microsoft的SQL「專家」,他們向我提供的唯一有用的信息是,他們指出,每當我得到這些間歇性超時錯誤時,「writelog」已經升高立即。

現在這提出了一個問題,因爲我不知道如何診斷這個問題。

我正在粘貼「通用」堆棧跟蹤,以便我可以從這裏的任何人那裏獲得一些有用的信息,他們是SQL Azure專家。

堆棧跟蹤:

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. This failure occurred while attempting to connect to the routing destination. The duration spent while attempting to connect to the original server was - [Pre-Login] initialization=2; handshake=9; [Login] initialization=0; authentication=0; [Post-Login] complete=1; ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)... `

+0

您是否實施了重試策略? – CSharpRocks

+0

還沒有,我認爲'Resiliency'是爲了幫助在Azure中彈出的「瞬態」錯誤? –

回答

0

實體框架支持配置的重試策略取決於服務器如何部署。當你正在部署到Azure中,應配置通過包括該代碼使用SQL Azure的策略:

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

由於在MSDN上記載:https://msdn.microsoft.com/en-us/data/dn456835.aspx?f=255&MSPPError=-2147217396

超時錯誤通常被認爲是SqlAzureExecutionStrategy中的暫時性錯誤。您可以在此處看到源代碼:https://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework.SqlServer/SqlAzureRetriableExceptionDetector.cs

如果您看到的超時錯誤未由SqlAzureExecutionStrategy默認處理,您可以通過繼承SqlAzureExecutionStrategy並重寫ShouldRetryOn來創建自己的代碼。 This blog post有一個很好的演練,做到這一點。