使用实体框架核心的MS DTC取消交易

时间:2020-02-09 18:25:42

标签: c# sql-server entity-framework-core

我收到一个

System.Data.SqlClient.SqlException(0x80131904):Microsoft分布式事务处理协调器(MS DTC)已取消了分布式事务处理。

使用EF Core 2.1时出现

错误。完整的堆栈跟踪位于此消息的底部。

这是基本过程:

  1. 从Kafka读取请求消息
  2. 将消息写到SQL Server上的一组“原始”表中
  3. 调用存储过程,该过程从原始表中读取消息,验证传入的数据,转换数据中的几个字段,然后写入一组“生产”表。
  4. 从生产表中检索转换后的请求。
  5. 通过主要是微服务的业务流程处理请求,并随着业务流程的进展将更新保存回生产表。

MSDTC错误在步骤3间歇性但频繁发生。步骤2和3中的原始表和生产表位于同一SQL Server上的单独数据库中。作为第5步的一部分,还有一个对单独的SQL Server的调用,但我得到的唯一错误是在第3步期间取消了MSDTC。

我知道.NET Core不支持MSDTC,但是我不确定为什么将此过程的这一部分提升为MSDTC。我没有任何显式事务,并且通过appsettings中的ConnectionStrings为每个数据库标准化了连接字符串。

任何见识表示赞赏。

authAPI.usp_AuthTransform @CorrelationID, @AuthStateID OUT, @CorrelationSeqID OUT

System.Data.SqlClient.SqlException (0x80131904): The Microsoft Distributed Transaction Coordinator (MS DTC) has cancelled the distributed transaction.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   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.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader()
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQuery(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)
ClientConnectionId:24a868f2-3f00-4e07-92fd-7537beb23d33
Error Number:1206,State:118,Class:18
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Private.CoreLib.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Private.CoreLib.dll
'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.15\System.IO.MemoryMappedFiles.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
nH.AuthRequestIntake.nHDischarge.ReceiveHandler:Error: 353c5648-5ef0-44b9-bd5b-ac1395243ac6-System.Data.SqlClient.SqlException (0x80131904): The Microsoft Distributed Transaction Coordinator (MS DTC) has cancelled the distributed transaction.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   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.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader()
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQuery(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommandAsync(DatabaseFacade databaseFacade, RawSqlString sql, IEnumerable`1 parameters, CancellationToken cancellationToken)
   at nH.AuthRequestIntake.Domain.AuthRequestReceivedEventHandler`2.TransformToAuthRequestAsync(AuthRequest rawRequest, IIncomingDbContext dbContext) in C:\Users\scott.carter\source\repos\authrequestintake\nH.AuthRequestIntake.Domain\AuthRequestReceivedEventHandler.cs:line 170
   at nH.AuthRequestIntake.Domain.AuthRequestReceivedEventHandler`2.HandleAsync(IDomainEvent domainEvent) in C:\Users\scott.carter\source\repos\authrequestintake\nH.AuthRequestIntake.Domain\AuthRequestReceivedEventHandler.cs:line 68
ClientConnectionId:24a868f2-3f00-4e07-92fd-7537beb23d33
Error Number:1206,State:118,Class:18

System.Data.SqlClient.SqlException (0x80131904): The Microsoft Distributed Transaction Coordinator (MS DTC) has cancelled the distributed transaction.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   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.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.CompleteAsyncExecuteReader()
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQuery(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommandAsync(DatabaseFacade databaseFacade, RawSqlString sql, IEnumerable`1 parameters, CancellationToken cancellationToken)
   at nH.AuthRequestIntake.Domain.AuthRequestReceivedEventHandler`2.TransformToAuthRequestAsync(AuthRequest rawRequest, IIncomingDbContext dbContext) in C:\Users\scott.carter\source\repos\authrequestintake\nH.AuthRequestIntake.Domain\AuthRequestReceivedEventHandler.cs:line 170
   at nH.AuthRequestIntake.Domain.AuthRequestReceivedEventHandler`2.HandleAsync(IDomainEvent domainEvent) in C:\Users\scott.carter\source\repos\authrequestintake\nH.AuthRequestIntake.Domain\AuthRequestReceivedEventHandler.cs:line 68
ClientConnectionId:24a868f2-3f00-4e07-92fd-7537beb23d33
Error Number:1206,State:118,Class:18```



1 个答案:

答案 0 :(得分:0)

事实证明这是一条红鲱鱼。我读过某个地方的MSDTC问题有时掩盖了其他问题,建议进行数据库跟踪。可以肯定的是,跟踪显示在负载下临时表正在通过连接删除,因此我们现在正在努力解决此问题。

感谢所有阅读并考虑了该问题的人!