【问题标题】:Entity Framework 6 Timeout exception and retry but the query succeeded实体框架6超时异常并重试但查询成功
【发布时间】:2017-03-20 04:10:27
【问题描述】:

我们在 Azure 云服务中使用实体框架 6.1.2。 我们已经添加了 RetryStrategy 和 CommitFailureHandler。

收到请求后,我们尝试使用实体框架的原始 SQL 部分将新行插入到表中。

我们首先检查是否存在与我们要插入的 ID 对应的行。

var rows = dbContext.Database.SqlQuery<T>(
    "SELECT * FROM FactState WHERE ID = @p0 ", ID
).ToList();

return rows != null;

如果不存在则插入该行

dbContext.Database.ExecuteSqlCommand(
    "INSERT INTO FactState ( ID, Value ) VALUES( @p0, @p1 )",
    ID,
    Value
);

插入查询每隔一段时间就会抛出一个超时异常,重试策略会正确重试。 但是,在重试时,查询会为每次重试引发主键违规,直到达到 5 的限制。

现在我的问题是这是怎么发生的。 我本来预计超时异常会导致根本不添加行,或者如果在提交阶段发生异常,那么提交失败处理程序将运行并正确识别查询已成功。

我已经在一个测试程序中逐步完成了 Entity Framework 的源代码,我可以通过在使用表锁强制超时后手动插入行来模拟行为。 但我不明白它是如何在我们的生产环境中发生的。

更新

周末又发生了多次超时异常。异常发生在提交过程中,但这意味着提交失败处理程序没有运行。我想它可能在提交运行时未附加提交失败处理程序,但是我不明白我如何使用以下代码添加提交失败处理程序

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

        this.SetTransactionHandler(
            SqlProviderServices.ProviderInvariantName,
            () => new CommitFailureHandler());
    }
}

发生错误时我已附加堆栈跟踪

Exception: System.Data.SqlClient.SqlException (0x80131904): 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=1; handshake=18; [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)
at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
at System.Data.SqlClient.TdsParserStateObject.TryReadByte(Byte& value)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalTransaction.Commit()
at System.Data.SqlClient.SqlTransaction.Commit()
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbTransactionDispatcher.Commit(DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.EntityClient.EntityTransaction.Commit()

【问题讨论】:

  • 您的生产在 VM 中使用 SQL Azure 数据库或 SQL Server?
  • 我们正在使用 SQL Azure 数据库

标签: c# entity-framework azure


【解决方案1】:

【讨论】:

  • 链接表示 ReliableSqlConnection 不再维护?目前我们只是使用实体框架来处理内置重试的连接。我想我不明白 ReliableSqlConnection 会提供什么实体框架还没有帮助解决这个问题。
  • 也许更好的主意是在 EF github 项目上报告这个问题。另外,请看这里:github.com/robdmoore/SQLAzureTransientDemo
  • 您好,感谢您提出的建议,我在 EF Github 页面上记录了一个问题。 github.com/aspnet/EntityFramework6/issues/218
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多