gpt4 book ai didi

c# - 让 EntityFramework 尊重 mysql max_user_connections

转载 作者:行者123 更新时间:2023-11-29 21:07:13 28 4
gpt4 key购买 nike

如果 max_user_connections 连接已打开,是否有方法告诉 Entity Framework 等待?

我想我可以捕获异常并重试或保留一个计数器,但这最多感觉很糟糕。

我的 Azure 日志中充满了以下消息:

System.Data.Entity.Core.EntityException: The underlying provider failed on Open. 
MySql.Data.MySqlClient.MySqlException:
Authentication to host 'xxx' for user 'yyy' using method 'mysql_native_password' failed with message:
User 'yyy' has exceeded the 'max_user_connections' resource (current value: 4)

我尝试将 Max Pool Size=4 添加到连接字符串,但这没有帮助。

<add name="DbContext" connectionString="Database=db;Data Source=xxx;User Id=yyy;Password=zzz;CharSet=utf8;Persist Security Info=True;Convert Zero Datetime=True;Max Pool Size=4" providerName="MySql.Data.MySqlClient" />

最佳答案

根据 Vito 的建议:捕获异常并让前端决定要做什么:

在后端,我使用了 PostSharpAspect-Oriented programming 的实现对于.NET。它允许我用一个属性来装饰我的所有存储库来处理这个特定的异常。

在前端,我可能会做一些类似等待 400 毫秒并在放弃之前重试 1-3 次的事情。

在包管理器控制台中:
安装包PostSharp

您还需要安装PostSharp plugin for Visual Studio

[Serializable]
public class MaxMysqlConnectionExceptionHandlerAspect : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
if (args.Exception.InnerException != null && args.Exception.InnerException.Message.Contains("Too many connections"))
{
args.FlowBehavior = FlowBehavior.ThrowException;
args.Exception = new Exception("MAX_CONNECTIONS");
}
}

public override Type GetExceptionType(MethodBase method)
{
return typeof(EntityException);
}
}

然后装饰类或方法:

[MaxMysqlConnectionExceptionHandlerAspect]
public class FancyPantsService {}

关于c# - 让 EntityFramework 尊重 mysql max_user_connections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36701895/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com