首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#模拟器“到多个连接”

C#模拟器“到多个连接”
EN

Stack Overflow用户
提问于 2013-08-19 16:25:16
回答 3查看 180关注 0票数 0

我最近收到了mysql to mysql错误,我使用了下面这样的sql查询

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

代码语言:javascript
复制
private static SqlDatabaseClient CreateClient(int Id)
{
     MySqlConnection Connection = new MySqlConnection(GenerateConnectionString());
     Connection.Open();

     return new SqlDatabaseClient(Id, Connection);
}

当我收到10-12个在线连接时,模拟器在调试器中运行了7-8个小时,被高亮显示的那行导致它崩溃的代码是connection.open();

EN

回答 3

Stack Overflow用户

发布于 2013-08-19 16:28:56

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

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2013-08-19 16:45:25

我建议重写成类似这样的内容:

代码语言:javascript
复制
  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
    });
  }
票数 0
EN

Stack Overflow用户

发布于 2013-08-19 16:46:40

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18309362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档