C# Emulator 'To Many Connections'

我是研究僧i 提交于 2019-12-13 06:28:48

问题


I have recently been receiving a mysql to many connectiions error, ive used sql queries such as the one below

SET GLOBAL max_conmnections = 8000; and ive also higherd the mysql.pool.max to 8000 and when my emulator is in the debugger, it crashes on this void

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

     return new SqlDatabaseClient(Id, Connection);
}

the line thats highlighted that has caused it to crash was connection.open(); it happens when i receive 10-12 online connections, the emulator was running for 7-8 hours in the debugger!


回答1:


What you can try is to close and dispose the connection and commands after used by the C# using statement:

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;
}



回答2:


I would suggest rewrite into something like:

  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
    });
  }



回答3:


I think you can add codes like: SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); then, after closing SqlDataReader's instance, the Connection object will also be closed.



来源:https://stackoverflow.com/questions/18309362/c-sharp-emulator-to-many-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!