How to handle transient failure in Entity Framework?

c# entity-framework-6 sql-server

Question

I would like to ask some issues. I have got some of entity exceptions.

I am using Entity Framework 6.2.0 in my C# Windows app and SQL Server 2017. My app is running with 8 client PCs and server.

I really don't know how to that exception is occur. Is it related to connection timeout? How to fix that?

I get this exception:

System.Data.Entity.Core.EntityException: An exception has been raised that is likely due to a transient failure.

If you are connecting to a SQL Azure database consider using SqlAzureExecutionStrategy.

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition.
See the inner exception for details.

System.Data.SqlClient.SqlException: Transaction (Process ID 58) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

Some code:

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);
}
1
0
3/6/2020 4:59:35 PM

Popular Answer

In the cloud environment you'll find that failed and dropped database connections happen periodically. That's partly because you're going through more load balancers compared to the on-premises environment where your web server and database server have a direct physical connection. Also, sometimes when you're dependent on a multi-tenant service you'll see calls to the service get slower or time out because someone else who uses the service is hitting it heavily. In other cases you might be the user who is hitting the service too frequently, and the service deliberately throttles you – denies connections – in order to prevent you from adversely affecting other tenants of the service.

source: https://docs.microsoft.com/en-us/aspnet/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps-with-windows-azure/transient-fault-handling

Here's how to fix using 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());
    }
}
0
3/6/2020 4:15:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow