6

我真的很感激任何建议,无论多么简单或复杂,以帮助我隔离和解决这个问题。

我有一些生成小报告文件的代码。对于集合中的每个文件,执行存储过程以通过 XML 阅读器获取数据(它是一个非常大的结果集)。当我创造了这一切并逐步完成它时,一切都很好。文件生成,没有错误。

该库通过远程调用,并通过 IIS 托管。当我部署编译库并调用它时,它能够生成一些报告,但随后会引发线程中止异常。如果我将调试器附加到 asp 工作进程,并逐步执行代码,我就没有问题。

看到这个失败是相当一致的,我寻找相似之处,发现失败发生在不同的报告上,但似乎发生在大约相同的时间点。

这让我认为这是调试器覆盖的超时设置,我对整个过程进行了一些粗略的计时(不是单个失败的代码),它似乎在大约 200 秒后就失败了。web.config executionTimeout 设置为 600 分钟(足够高)。此服务器应用程序的其他部分需要 COM+ 事务(2 分钟超时),但这不是其中之一。我不知道它可能会达到什么超时(大约 200 秒大关)。

SQL 连接超时默认保留(连接打开成功),命令超时为 300 秒(执行命令仅需 12-15 秒)。

  • 还有其他我可能会错过的超时吗?

我运行了 SQL 探查器,它显示正确返回了结果(所有语句和 RPC 都已完成 - 没有错误)。通过 SSMS 执行代码提供了完美的结果。

使用反射器,我深入研究了 SNINativeMethodWrapper,它是非托管代码的包装器,但我看不出它实际上在做什么。我只能假设(可能是错误的)代码已经从 SQL 服务器接收到 TDS,并且包装器正在尝试获取与数据包关联的连接,但它不能。

  • 有谁知道这个包装器应该做什么?
  • 有没有办法跟踪/调试此代码以找出导致失败的原因?

我尝试使用不同的方法(ExecScalar、DataAdapter),但它们都在内部使用 ExecuteReader。

我尝试禁用连接池并强制客户端使用与服务器相同的数据包大小。

  • 是否有人对导致此问题的原因有任何想法,或者我可以做些什么来隔离并尝试纠正问题?

这是生成异常的调用代码。

Private Function GetDataAsXmlDoc(ByVal cmd As SqlClient.SqlCommand) As XmlDocument

    Dim _xmlDoc As XmlDocument

    Using _connection As New SqlClient.SqlConnection(GetConnectionString())

        Logging.DebugEvent.Raise(Me.GetType.Namespace, Reflection.MethodBase.GetCurrentMethod().Name, _
                                 "No cached data found or used. Getting data for report from the database using SQL connection.")

        Dim _xmlReader As XmlReader
        'DataAdapter,ExecuteScalar, ExecuteXmlReader all use ExecuteReader internally and suffer the same problem.'
        'If you dont believe me, reflect it or look at one of the blowed up stack traces. '

        '_connection.ConnectionString += ";Pooling=false;"' 'This has no effect on the ThreadAbort.'
        cmd.Connection = _connection
        cmd.CommandTimeout = 300
        _connection.Open()

        Logging.DebugEvent.Log(String.Format("Connection opened, using packet size of {0}.", _connection.PacketSize))

        _xmlReader = cmd.ExecuteXmlReader() 'Thread aborts in here'

        Logging.DebugEvent.Raise(Me.GetType.Namespace, Reflection.MethodBase.GetCurrentMethod().Name, _
                                 "Report data recieved from database")

        _xmlDoc = New XmlDocument()
        _xmlDoc.Load(_xmlReader)

        _xmlReader.Close()

    End Using

  Return _xmlDoc

End Function

Exception String - System.Threading.ThreadAbortException: Thread was being aborted.
   at SNINativeMethodWrapper.SNIPacketGetConnection(IntPtr packet)
   at System.Data.SqlClient.TdsParserStateObject.ProcessSniPacket(IntPtr packet, UInt32 error)
   at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
   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.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteXmlReader()...
4

1 回答 1

2

我相信我已经解决了这个问题。上面示例中的违规代码行是语句...

 Logging.DebugEvent.Raise(Me.GetType.Namespace, Reflection.MethodBase.GetCurrentMethod().Name, _
                                 "Report data recieved from database")

这是对应用程序块(MS 企业库)的调用,用于将事件记录到平面文件(在这种情况下)或事件日志。

This one, in between the ExecuteXMLReader() and the actual usage of the reader in the XML document, was sometimes failing harshly, causing the whole thread to abort. I moved the line to after the _xmlReader.Close() and it took care of the problem.

于 2009-01-14T14:26:19.290 回答