用設定檔配置 SqlClient 重試邏輯

適用於: .NET Framework .NET .NET 標準

下載 ADO.NET

使用應用程式組態檔,為該處理程序中的每個 SqlConnection 或 SqlCommand 執行個體指派預設重試提供者。 若沒有這些區段或物件層級的提供者指派,SqlClient 會使用 SqlConfigurableRetryFactory.CreateNoneRetryProvider 且不會重試。

組態區段

在 configSections 元素內宣告重試區段。 區段聲明必須出現在對應的設定值之前。

  • SqlConfigurableRetryLogicConnection:設定 SqlConnection 的預設提供者。
<section name="SqlConfigurableRetryLogicConnection"
        type="Microsoft.Data.SqlClient.SqlConfigurableRetryConnectionSection, Microsoft.Data.SqlClient"/>
  • SqlConfigurableRetryLogicCommand:設定為 SqlCommand 的預設提供者。
<section name="SqlConfigurableRetryLogicCommand"
        type="Microsoft.Data.SqlClient.SqlConfigurableRetryCommandSection, Microsoft.Data.SqlClient"/>

連線區段

透過以下屬性設定應用程式中所有 SqlConnection 實例的預設重試邏輯:

  • numberOfTries:設定總嘗試次數,包括初始操作。 有效範圍為1到60。

  • deltaTime:將間隙時間區間設為物件 TimeSpan 。

  • minTime:設定物件允許的最小間隙時間區間 TimeSpan 。

  • maxTime:設定物件允許的最大間隙時間區間 TimeSpan 。

  • transientErrors:設定一個逗號分隔的錯誤編號清單以重新嘗試。 如果省略它,提供者會使用 內建的瞬態錯誤清單。 如果你指定了,你的清單就會取代內建清單。

  • retryMethod:指定一個重試方法建立器,透過參數 SqlRetryLogicOption 接收重試設定並回傳物件 SqlRetryLogicBaseProvider 。

  • retryLogicType:設定一個自訂的重試邏輯提供者,包含由 retryMethod所識別的重試方法創建者。 這些方法必須符合 的 retryMethod條件。 請使用提供者的完整限定型別名稱。 如需詳細資訊,請參閱 指定完整類型名稱。

注意

當你使用內建提供者時,不需要指定 retryLogicType。 關於可用的方法,請參閱 SqlClient 內建的重試邏輯提供者。

命令區

透過使用連接區段屬性及以下命令專屬屬性,設定應用程式中所有 SqlCommand 實例的預設重試邏輯:

  • authorizedSqlCondition:設定一個必須匹配的正規表達式 SqlCommand.CommandText ,提供者才會重試該指令。 如果表達式不匹配,指令會執行一次且不重試邏輯。

注意

正則表達式具有區分大小寫。 當您需要不區分大小寫的比對時,請加入內嵌選項,例如 (?i)。

範例

  • 透過使用 SqlConfigurableRetryFactory.CreateFixedRetryProvider 方法與預設暫時性錯誤清單,嘗試建立連線達三次,各嘗試之間的延遲大約為 1 秒:

    <SqlConfigurableRetryLogicConnection retryMethod ="CreateFixedRetryProvider"
                                            numberOfTries ="3" deltaTime ="00:00:01"/>
    
  • 透過使用 SqlConfigurableRetryFactory.CreateExponentialRetryProvider 方法與預設暫時性錯誤清單,嘗試建立連線達五次,各嘗試之間的延遲最長為 45 秒:

    <SqlConfigurableRetryLogicConnection retryMethod ="CreateExponentialRetryProvider"
                        numberOfTries ="5" deltaTime ="00:00:03" maxTime ="00:00:45"/>
    
  • 透過使用 SqlConfigurableRetryFactory.CreateIncrementalRetryProvider 方法和預設暫時性錯誤清單,嘗試執行命令達四次,延遲時間為 2 到 30 秒:

    <SqlConfigurableRetryLogicCommand retryMethod ="CreateIncrementalRetryProvider"
                        numberOfTries ="4" deltaTime ="00:00:02" maxTime ="00:00:30"/>
    
  • 嘗試執行命令達八次,延遲時間從一秒到一分鐘。 此限制僅適用於包含 CommandText 的命令,且這些命令含有大寫單字 SELECT 以及錯誤編號 102 或 997。 指定 transientErrors 取代了內建的清單,因此不會重試其他錯誤。 下列範例會使用 SqlConfigurableRetryFactory.CreateIncrementalRetryProvider:

    <SqlConfigurableRetryLogicCommand retryMethod ="CreateIncrementalRetryProvider"
                            numberOfTries ="8" deltaTime ="00:00:01" maxTime ="00:01:00"
                            transientErrors="102, 997"
                            authorizedSqlCondition="\b(SELECT)\b"/>
    

注意

在以下兩個範例中,您可以從 SqlClient 中的可設定重試邏輯核心 API 找到自訂重試邏輯原始程式碼。 假設 CreateCustomProvider 方法定義於應用程式執行目錄中的 CustomCRL_Doc.CustomRetry 元件中的 CustomCRL_Doc.dll 類別中。

  • 嘗試建立最多五次的連線,延遲時間為 3 到 45 秒,清單中的錯誤號碼為 4060、997 和 233,且使用指定的自訂重試提供者:

    <SqlConfigurableRetryLogicConnection retryLogicType ="CustomCRL_Doc.CustomRetry, CustomCRL_Doc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                        retryMethod ="CreateCustomProvider"
                        numberOfTries ="5" deltaTime ="00:00:03" maxTime ="00:00:45"
                        transientErrors ="4060, 997, 233"/>
    
  • 此範例的行為與上一個範例類似:

    <SqlConfigurableRetryLogicConnection retryLogicType ="CustomCRL_Doc.CustomRetry, CustomCRL_Doc"
                        retryMethod ="CreateCustomProvider"
                        numberOfTries ="5" deltaTime ="00:00:03" maxTime ="00:00:45"
                        transientErrors ="4060, 997, 233"/>
    

Important

對任一已設定的預設值提出第一次要求時,會使 SqlClient 載入並快取連線和命令提供者。 設定檔的變更在程序重新啟動前不會影響任一提供者。

讀取重試設定時的錯誤不會讓應用程式失敗。 SqlClient 會追蹤設定錯誤並使用 SqlConfigurableRetryFactory.CreateNoneRetryProvider,因此操作可以不重試。

使用事件來源追蹤來驗證或排除重試設定的故障。 如需詳細資訊,請參閱 在 SqlClient 中啟用事件追蹤。