簡體   English   中英

什么統計值對應於 ADO.net SqlCommand.CommandTimeout 屬性?

[英]What statistics value corresponds to the ADO.net SqlCommand.CommandTimeout property?

我試圖理解這個屬性,我有一個在 ADO.net 中運行 sql 查詢的函數,我將 SqlCommand.ConnectTimeout 屬性設置為 500 秒,將 SqlCommand.CommandTimeout 屬性設置為 1 秒。 大致相關部分如下所示。

        var conn = dbConn as SqlConnection;
        conn.ConnectTimeout = 500;
        conn.StatisticsEnabled = true;

        try
        {
            using (var command = new SqlCommand(query, conn))
            {
                command.CommandTimeout = 1;
                using (var adapter = new SqlDataAdapter(command))
                {
                    var rowsCount = adapter.Fill(startIdx, MaxRows, new DataTable[] { dtResult });
                    var stats = conn.RetrieveStatistics();
                    long commandExecutionTimeInMs = (long)stats["ExecutionTime"];
                    long commandNetworkServerTimeInMs = (long)stats["NetworkServerTime"];
                    Console.Writeline("Command statistics execution time: " + commandExecutionTimeInMs.ToString());
                    Console.Writeline("Command statistics network server time: " + commandNetworkServerTimeInMs.ToString());
                    conn.ResetStatistics();
                    conn.StatisticsEnabled = false;
                    return rowsCount;
                }
            }
        }
        catch (Exception ex)
        {
            var stats = conn.RetrieveStatistics();
            long commandExecutionTimeInMs = (long)stats["ExecutionTime"];
            long commandNetworkServerTimeInMs = (long)stats["NetworkServerTime"];
            Console.Writeline("Command statistics execution time: " + commandExecutionTimeInMs.ToString());
            Console.Writeline("Command statistics network server time: " + commandNetworkServerTimeInMs.ToString());
            conn.ResetStatistics();
            conn.StatisticsEnabled = false;
        } 

有了這個,我預計任何超過 1 秒的執行時間都會超時並拋出異常並顯示以下消息:“執行超時已過期。在操作完成之前超時時間已過,或者服務器沒有響應。”。 但是它非常不一致,我的日志顯示執行時間可以達到幾秒甚至 10+ 秒,而有時不會拋出任何東西。 並且 NetworkServerTime 始終在 200 到 300 毫秒范圍內。

那么這里的問題是什么? 我誤解了 CommandTimeout 的工作原理嗎? 我在看錯誤的統計值嗎? 我希望將 CommandTimeout 設置為 1,然后當我的查詢超過這個時間時,我可以清楚地看到一個統計信息,在 catch 塊中將時間記錄為 1 秒或 1000 毫秒

參考此頁面似乎沒有其他相關https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/provider-statistics-for-sql-server

文檔中有注釋和 2 個注釋。

評論

!Note

The CommandTimeout property will be ignored by older APM (Asynchronous Programming Model) asynchronous method calls such as BeginExecuteReader. It will be honored by newer TAP (Task Asynchronous Programming) methods such as ExecuteReaderAsync.

!Note

This property is the cumulative time-out (for all network packets that are read during the invocation of a method) for all network reads during command execution or processing of the results. A time-out can still occur after the first row is returned, and does not include user processing time, only network read time.

For example, with a 30 second time out, if Read requires two network packets, then it has 30 seconds to read both network packets. If you call Read again, it will have another 30 seconds to read any data that it requires.

在查看了有關我使用的方法的文檔和討論之后,我想我找到了答案。 對於使用 SqlDataAdapter 的人來說,這似乎不是一個新問題。 在這里引用微軟工程師的話:

SqlCommand.CommandTimeout 屬性僅影響命令的單個操作。 因此,超時設置將是單個執行期間所有網絡讀取的總時間,例如 SqlCommand.ExecuteReader 或 SqlDataReader.Read()、SqlDataReader.GetString()。 每次調用新操作時都會重置超時。

SqlDataAdapter 使用命令的公共接口,因此它調用 ExecuteReader 一次,然后根據需要調用多個 DataReader 操作來檢索數據。 所以總時間是(一次調用 ExecuteReader + 對命令的其他操作)X CommandTimeout,具體取決於參數。

參考: https : //social.msdn.microsoft.com/Forums/en-US/de557eb1-aa0f-403d-89c6-199bb318a08f/sqldataadapterfill-does-not-time-out-when-it-should?forum=adodotnetdataproviders

我正在使用的 SqlDataAdapter.Fill() 方法可能正在執行多個操作,因此它很容易超過執行時間方面的超時但實際上並未觸發異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM