0

我最近收到一个 mysql to many connectiions 错误,我使用了如下的 sql 查询

SET GLOBAL max_conmnections = 8000;我也提高了mysql.pool.max to 8000,当我的模拟器在调试器中时,它在这个空白处崩溃

private static SqlDatabaseClient CreateClient(int Id)
{
     MySqlConnection Connection = new MySqlConnection(GenerateConnectionString());
     Connection.Open();

     return new SqlDatabaseClient(Id, Connection);
}

突出显示导致它崩溃的那一行是connection.open();当我收到 10-12 个在线连接时发生的,模拟器在调试器中运行了 7-8 小时!

4

3 回答 3

1

您可以尝试在 C# using语句使用后关闭和处理连接和命令:

private static SqlDatabaseClient CreateClient(int Id)
{
    Int32 returnId = 0;

    try
    {
      using(MySqlConnection connection = new MySqlConnection(GenerateConnectionString()))
      {
        connection.Open();

        if(connection.State == ConnectionState.Open)
        {
           returnId = Id;
        }

      }
    }
    catch(Exception exception)
    {
       Console.Write(ex.Message);
    }
    finally
    {
        if(connection.State == ConnectionState.Open)
        {
           connection.Close();

        }
    }

    return returnId;
}
于 2013-08-19T08:28:56.230 回答
0

我建议重写为:

  private static void ExecuteInClientContext(int Id, Action<SqlDatabaseClient> callback) {
    if (callback == null) {
      throw new ArgumentNullException("callback");
    }

    using(MySqlConnection Connection = new MySqlConnection(GenerateConnectionString())) {
      Connection.Open();
      callback.Invoke(new SqlDatabaseClient(Id, Connection));
    }
  }

  static void Foo() {
    ExecuteInClientContext(1, (context) => {
      // whatever
    });
  }
于 2013-08-19T08:45:25.050 回答
0

我认为您可以添加如下代码: SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 然后,在关闭 SqlDataReader 的实例后,Connection 对象也将被关闭。

于 2013-08-19T08:46:40.103 回答