简体   繁体   English

SQL Server,C#:事务回滚的超时异常

[英]SQL Server, C#: Timeout exception on Transaction Rollback

I've got a strange problem. 我有一个奇怪的问题。 I have a .NET program and my process logic needs a long-running transaction (~20min) on a SQL Server 2005 database. 我有一个.NET程序,我的进程逻辑需要一个SQL Server 2005数据库上长时间运行的事务(~20分钟)。 That's ok, since nobody accesses the database in parallel. 没关系,因为没有人并行访问数据库。 When something goes wrong, the transaction should be rolled back. 当出现问题时,应该回滚事务。

Infrequently and without any visible pattern the Rollback() operation on my DbTransaction object throws a SqlException : 很少,没有任何可见的图案Rollback()我在操作DbTransaction对象将抛出SqlException

Message: "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

StackTrace:
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
   at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParserStateObject.ReadPacket(Int32 bytesExpected)
   at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
   at System.Data.SqlClient.TdsParserStateObject.ReadByte()
   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.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalTransaction.Rollback()
   at System.Data.SqlClient.SqlTransaction.Rollback()

I don't know if it's really a timeout problem due to the fact, that the code works sometimes and sometimes not. 我不知道它是否真的是一个超时问题,因为这个代码有时会运行,有时也不会运行。 Furthermore the only timeouts I know are ConnectionTimeout and CommandTimeout , but obviously those aren't the problem in this case. 此外,我知道的唯一超时是ConnectionTimeoutCommandTimeout ,但显然在这种情况下这些不是问题。

Does anyone have an idea about this problem? 有没有人对这个问题有所了解?

Thanks a lot, Matthias 非常感谢,马蒂亚斯

Matt Neerincx of the Sql Server team addressed this in an MSDN forum question . Sql Server团队的Matt Neerincx在一个MSDN论坛问题中解决了这个问题 Odd but true, the connect timeout from the connection string is used to set the timeout. 奇数但是为真,连接字符串的连接超时用于设置超时。 Verified by him looking at the source code. 他通过查看源代码验证。

Transactions can take a while to roll-back; 交易可能需要一段时间才能回滚; if that takes too long, sure you'll get a timeout. 如果这需要太长时间,请确保你会超时。 There doesn't seem to be an obvious way to influence this - you could try managing the transaction via TSQL - then you can (ab)use the CommandTimeout - but it could simply be that it takes a little while if you are making lots of changes inside the transaction; 似乎没有一种明显的方法来影响这一点 - 你可以尝试通过TSQL管理事务 - 然后你可以(ab)使用CommandTimeout - 但它可能只是需要一点时间,如果你正在做很多交易内部的变化; SQL Server assumes that most things will run to completion, so "commit" is virtually free, while "rollback" is more expensive. SQL Server 假设大多数事情都将运行完成,因此“提交”几乎是免费的,而“回滚”则更昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM