【问题标题】:MySql Entity Framework - Connect Timeout expiredMySql 实体框架 - 连接超时已过期
【发布时间】:2021-03-14 07:56:03
【问题描述】:

我正在使用来自 C# .NET-core 2.2 和实体框架 6 应用程序的 Google Cloud SQL 和 MySql v5.7。

在我的日志中,我可以从我使用数据库的代码中的多个位置看到以下异常:

MySql.Data.MySqlClient.MySqlException (0x80004005): Connect Timeout expired. ---> System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at System.Threading.SemaphoreSlim.WaitUntilCountOrTimeoutAsync(TaskNode asyncWaiter, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs:line 42
   at MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 507
   at MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 523
   at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 232
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)

当数据库上有一些负载(不是很高,大约是数据库机器的 20% cpu)时,这种情况会在瞬间发生。

配置上下文:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseMySql(
            new System.Net.NetworkCredential(string.Empty, ConfigurationManager.CacheCS).Password, builder =>
            {
                builder.EnableRetryOnFailure(15, TimeSpan.FromSeconds(30), null);
            }
            );
    }
}

这将设置最多 15 次重试,并且重试之间的最长间隔为 30 秒。

从日志看来,MySqlConnector 不会重试此特定错误。

我的尝试

尝试将临时错误编号添加到要添加的错误编号列表中:

builder.EnableRetryOnFailure(15, TimeSpan.FromSeconds(30), MySqlErrorCodes.TransientErrors);

其中MySqlErrorCodes.TransientErrors 定义为:

 public enum MySqlErrorCode
    {
        // Too many connections
        ConnectionCountError = 1040,
        // Unable to open connection
        UnableToConnectToHost = 1042,
        // Lock wait timeout exceeded; try restarting transaction
        LockWaitTimeout = 1205,
        // Deadlock found when trying to get lock; try restarting transaction
        LockDeadlock = 1213,
        // Transaction branch was rolled back: deadlock was detected
        XARBDeadlock = 1614

    }

    public class MySqlErrorCodes
    {
        static MySqlErrorCodes()
        {
            TransientErrors = new HashSet<int>()
            {
                (int)MySqlErrorCode.ConnectionCountError,
                (int)MySqlErrorCode.UnableToConnectToHost,
                (int)MySqlErrorCode.LockWaitTimeout,
                (int)MySqlErrorCode.LockDeadlock,
                (int)MySqlErrorCode.XARBDeadlock
            };
        }

        public static HashSet<int> TransientErrors { get; private set; }
    }

这不起作用。

问题

我该如何解决这个问题? 有没有办法让 Entity Framework 对此类连接问题更具弹性?

编辑

当我使用此代码执行原始 sql 命令来调用存储过程时会出现此问题:

public static async Task<RelationalDataReader> ExecuteSqlQueryAsync(this DatabaseFacade databaseFacade,
                                                     string sql,
                                                     CancellationToken cancellationToken = default(CancellationToken),
                                                     params object[] parameters)
{

    var concurrencyDetector = databaseFacade.GetService<IConcurrencyDetector>();

    using (concurrencyDetector.EnterCriticalSection())
    {
        var rawSqlCommand = databaseFacade
            .GetService<IRawSqlCommandBuilder>()
            .Build(sql, parameters);

        return await rawSqlCommand
            .RelationalCommand
            .ExecuteReaderAsync(
                databaseFacade.GetService<IRelationalConnection>(),
                parameterValues: rawSqlCommand.ParameterValues,
                cancellationToken: cancellationToken);
    }
}

...

using (var context = new CacheDbContext())
            {
                using (var reader = await context
                    .Database
                    .ExecuteSqlQueryAsync("CALL Counter_increment2(@p0, @p1, @p2)",
                        default(CancellationToken),
                        new object[] { id, counterType, value })
                    .ConfigureAwait(false)
                    )
                {
                    reader.DbDataReader.Read();
                    if (!(reader.DbDataReader[0] is DBNull))
                        return Convert.ToInt32(reader.DbDataReader[0]);
                    else
                    {
                        Logger.Error($"Counter was not found! ('{id}, '{counterType}')");
                        return 1;
                    }
                }
            }

我认为这可能是连接超时没有重试的原因。

如何在不执行两次相同的存储过程的情况下安全地重试?

编辑

这些是全局变量:

显示全局变量,例如“%timeout%”

connect_timeout 10
delayed_insert_timeout  300
have_statement_timeout  YES
innodb_flush_log_at_timeout 1
innodb_lock_wait_timeout    50
innodb_rollback_on_timeout  OFF
interactive_timeout 28800
lock_wait_timeout   31536000
net_read_timeout    30
net_write_timeout   60
rpl_semi_sync_master_async_notify_timeout   5000000
rpl_semi_sync_master_timeout    3000
rpl_stop_slave_timeout  31536000
slave_net_timeout   30
wait_timeout    28800

显示全局状态,例如“%timeout%”

Ssl_default_timeout 7200
Ssl_session_cache_timeouts  0

显示全局状态,例如“%uptime%”

Uptime  103415
Uptime_since_flush_status   103415

除了连接超时问题,我还看到以下日志:

MySql.Data.MySqlClient.MySqlException (0x80004005): MySQL Server rejected client certificate ---> System.IO.IOException: Unable to read data from the transport connection: Broken pipe. ---> System.Net.Sockets.SocketException: Broken pipe

这似乎是与数据库连接相关的问题。

重试此类异常是否安全?

【问题讨论】:

  • 从您的 MySQL 命令提示符中,请发布 A) SHOW GLOBAL VARIABLES LIKE '%timeout%; 的 TEXT 结果和 B) 显示全局状态,如 '%timeout%;和 C) 显示 '%uptime%' 之类的全局状态;
  • 我认为在 Cloud Sql 中无法登录机器
  • 你有办法进入 MySQL 命令提示符运行 SELECT NOW();返回一天中的时间?或显示数据库; ?
  • 如果不能,请让 Google 支持为您运行查询并与您分享结果。
  • 这可能是代理和数据库实例之间的连接问题没有正确终止连接吗?

标签: c# mysql entity-framework .net-core google-cloud-sql


【解决方案1】:

由于连接字符串中maximumpoolsize 的值较低,因此发生此问题。

当有多个线程使用数据库并且没有足够的连接来处理所有请求时,这可能会导致连接超时。

要解决此问题,请将连接字符串中的此项更改为更高的值:

Max Pool Size={maxConnections};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2015-12-01
    • 2018-03-02
    相关资源
    最近更新 更多