从探查器跟踪中我们观察到使用了相同的连接
对于每个查询数据库查询。这是设计使然,正如所讨论的
早期,即当开发人员显式打开连接时
告诉 EF 不要为每个命令打开/重新打开连接。
嗯,这听起来肯定不像是一般性的说法。什么分析器跟踪?为什么假设连接由开发人员显式打开并由 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;
}
}
EDIT
好的,经过一段时间的记录后,我可以看出该策略基于
if (err.Number == 19)
is wrong。此错误的实际 SqlException 对象有ErrorCode = -2146232060
and 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;
}
}
EDIT 2:
有用。不再Physical connection is not usable
根本没有错误,并且没有 RetryLimitExceededException,所以这个错误实际上是暂时的(可以通过重试解决),所以我认为它应该包含在SqlAzureExecutionStrategy
.