From the profiler trace we observe that the same connection is used for each query database query. This is by design and as discussed early, i.e. when a connection is explicitly opened by the developer it tells EF not to open/reopen a connection for each command.
那麼這聽起來不像一般的聲明。什麼性能跟蹤器?爲什麼假設開發人員明確打開連接並處理EF?在原始問題中我沒有看到類似的東西(對EF來說這不是常見的做法)。
所以這些問題仍然沒有答案:爲什麼這不是由SqlAzureExecutionStrategy處理?編寫一個自己的DbExecutionStrategy來處理這個問題是一個好主意嗎?
由於我偶爾可以在Azure服務中看到此錯誤,因此我決定對其進行測試。這裏是我的策略:
public class ExtendedSqlAzureExecutionStrategy : SqlAzureExecutionStrategy
{
public ExtendedSqlAzureExecutionStrategy(int maxRetryCount, TimeSpan maxDelay) : base(maxRetryCount, maxDelay)
{ }
protected override bool ShouldRetryOn(Exception exception)
{
return base.ShouldRetryOn(exception) || IsPhysicalConnectionNotUsableSqlException(exception);
}
private bool IsPhysicalConnectionNotUsableSqlException(Exception ex)
{
var sqlException = ex as SqlException;
if (sqlException != null)
{
// Enumerate through all errors found in the exception.
foreach (SqlError err in sqlException.Errors)
{
if (err.Number == 19)
{
return true;
}
}
}
return false;
}
}
編輯
好了,所以一段時間,登錄後,我可以告訴大家,在戰略基礎上
if (err.Number == 19)
是錯誤。此錯誤的實際SqlException對象有ErrorCode = -2146232060
和Number = -1
- 我找不到任何文檔,因此我決定不在他們的基礎上制定策略。現在我想平凡的檢查:
public class ExtendedSqlAzureExecutionStrategy : SqlAzureExecutionStrategy
{
public ExtendedSqlAzureExecutionStrategy(int maxRetryCount, TimeSpan maxDelay) : base(maxRetryCount, maxDelay)
{ }
protected override bool ShouldRetryOn(Exception exception)
{
return base.ShouldRetryOn(exception) || IsPhysicalConnectionNotUsableSqlException(exception);
}
private bool IsPhysicalConnectionNotUsableSqlException(Exception ex)
{
var sqlException = ex as SqlException;
if (sqlException != null)
{
return sqlException.Message.Contains("Physical connection is not usable");
}
return false;
}
}
編輯2:
它的工作原理。沒有更多的Physical connection is not usable
錯誤,並且沒有RetryLimitExceededException,所以這個錯誤實際上是暫時的(可以通過重試解決),所以我認爲它應該包含在SqlAzureExecutionStrategy
中。
我真的希望有人回答這個問題。對我而言,這是Azure中的「預期和正常」,但SqlAzureExecutionStrategy不處理這個特定的錯誤。 – 2014-11-15 13:09:51
報告錯誤。該項目是開源的。承諾每天都在進行。 – usr 2014-11-16 18:05:18
首先它是一個錯誤嗎?這是經常發生的事情,我感覺它是一個設計決定。 – 2014-11-16 18:34:52