SqlDataReader.GetValue Hangs

耗尽温柔 提交于 2019-12-08 05:11:24

问题


I run a process built in C# .Net 4.6.1 that accesses a SQL Server database. The process runs on Windows Server 2012 R2 Standard. The SQL Server database is on another server with SQL Server version 11.0.6567.0

The process executes the following code:

01  var readStatement = “select * from TableA order by ColumnX,ColumnY, ColumnZ offset 0 rows”;   // TableA has 13 million rows
02  var readCommand = new SqlCommand(readStatement, myConnection) { CommandTimeout = 72000 };
03  var reader = readCommand.ExecuteReader();
04  while (reader.Read())
05  {
06     foreach (var columnName in columnNames)
07     {
08        Console.WriteLine(“Checkpoint 1”);
09        var value = reader[columnName];
10        Console.WriteLine(“Checkpoint 2”);
11     }
12  }

This process normally completes with no problem. Occasionally, it goes into line 09 (a call to SqlDataReader.GetValue) and never returns. I was able to witness this problem while running a trace on the database. The process went to that call at about row 8 million (out of the 13 million), but never returned. At the time it hung, the database trace reported “Batch Completed” and then “Audit Logout”. I am unable to replicate the problem in the debugger to see how deep into SqlDataReader.GetValue the process is hanging. It does not run into memory issues or other resource constrain

Here is the call stack to where the process hangs:

System.Data.dll!SNINativeMethodWrapper.SNIReadSyncOverAsync Normal [Managed to Native Transition]
System.Data.dll!SNINativeMethodWrapper.SNIReadSyncOverAsync(System.Runtime.InteropServices.SafeHandle pConn, ref System.IntPtr packet, int timeout)
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.TryReadByteArray(byte[] buff, int offset, int len, out int totalRead)
System.Data.dll!System.Data.SqlClient.TdsParserStateObject.TryReadDouble(out double value)
System.Data.dll!System.Data.SqlClient.TdsParser.TryReadSqlValueInternal(System.Data.SqlClient.SqlBuffer value, byte tdsType, int length, System.Data.SqlClient.TdsParserStateObject stateObj)
System.Data.dll!System.Data.SqlClient.TdsParser.TryReadSqlValue(System.Data.SqlClient.SqlBuffer value, System.Data.SqlClient.SqlMetaDataPriv md, int length, System.Data.SqlClient.TdsParserStateObject stateObj, System.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionOverride, string columnName)
System.Data.dll!System.Data.SqlClient.SqlDataReader.TryReadColumnInternal(int i, bool readHeaderOnly)
System.Data.dll!System.Data.SqlClient.SqlDataReader.TryReadColumn(int i, bool setTimeout, bool allowPartiallyReadColumn)
System.Data.dll!System.Data.SqlClient.SqlDataReader.GetValueInternal(int i)
System.Data.dll!System.Data.SqlClient.SqlDataReader.GetValue(int i)

来源:https://stackoverflow.com/questions/45987876/sqldatareader-getvalue-hangs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!