簡體   English   中英

如何處理實體框架中的瞬時故障?

[英]How to handle transient failure in Entity Framework?

我想問一些問題。 我有一些實體異常。

我在我的 C# Windows 應用程序和 SQL Server 2017 中使用 Entity Framework 6.2.0。我的應用程序在 8 台客戶端 PC 和服務器上運行。

我真的不知道異常是如何發生的。 是否與連接超時有關? 如何解決?

我得到這個例外:

System.Data.Entity.Core.EntityException:已引發異常,可能是由於瞬態故障。

如果要連接到 SQL Azure 數據庫,請考慮使用 SqlAzureExecutionStrategy。

System.Data.Entity.Core.EntityCommandExecutionException:執行命令定義時出錯。
有關詳細信息,請參閱內部異常。

System.Data.SqlClient.SqlException:事務(進程 ID 58)在鎖定時被死鎖 | 與另一個進程的通信緩沖區資源並已被選為死鎖受害者。 重新運行事務。

一些代碼:

try
{
    using (DataCtx.RTAD_BusinessLicenseEntities ctx = new DataCtx.RTAD_BusinessLicenseEntities())
    {
        err = "if(rv.TransactionStatus)=>InsertMode: " + this.txtSearchNRC.Text + ", " + this.TransactionID + ", " + paraChalenNumber.Value.ToString();
        ctx.SP_Vehicle_ApproveForExtendOperatorLicense(Helper.convertToAnscii(this.txtSearchNRC.Text), this.TransactionID, paraChalenNumber, true, true, "Approved");
    }
}
catch (Exception ex)
{
    Helper.WriteErrorLog("User Input => " + err + "\n\n" + ex.ToString());
    MessageBox.Show(ex.ToString(), "btnApply_Click_Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

在雲環境中,您會發現失敗和斷開的數據庫連接定期發生。 這部分是因為與 Web 服務器和數據庫服務器具有直接物理連接的本地環境相比,您要使用更多的負載平衡器。 此外,有時當您依賴多租戶服務時,您會看到對服務的調用變慢或超時,因為使用該服務的其他人正在大量使用它。 在其他情況下,您可能是頻繁訪問該服務的用戶,而該服務故意限制您(拒絕連接)以防止您對該服務的其他租戶產生不利影響。

來源: https : //docs.microsoft.com/en-us/aspnet/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps-with-windows-azure/transient-故障處理

以下是使用 EF 6 修復的方法:

// EF follows a Code based Configuration model and will look for a class that
// derives from DbConfiguration for executing any Connection Resiliency strategies
public class EFConfiguration : DbConfiguration
{
    public EFConfiguration()
    {
        AddExecutionStrategy(() => new SqlAzureExecutionStrategy());
    }
}

暫無
暫無

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

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