【问题标题】:MySQL Connection timeout on C#C#上的MySQL连接超时
【发布时间】:2017-04-08 13:34:33
【问题描述】:

我只是检查了有关连接超时的堆栈溢出的其他帖子,但看起来每个人都使用它修复了它

com.CommandTimeout = int.MaxValue;

dbaccess.ServerAddress = "Server=mysql;Database=MyDB;Connect Timeout=2147483;Uid=username;Pwd=mypassword";

很遗憾,40 秒后我仍然收到此错误消息:

在操作完成之前超时时间已过或服务器没有响应。

您可以看一下这两个屏幕截图。

MySQL Workbench 工作正常,执行相同查询需要 48 秒。

ErrorMessage

Workbench

我不能使用限制来减少获取的子集,因为我将信息集中在不同的表中。由于信息也存储在不同的mysql服务器中,因此无法使用存储过程。

我们将不胜感激任何反馈。

费尔南多

编辑:

这是代码。我发布了图像以显示执行时间,而不是错误消息。

public DataSet ExecuteQuery(string[] Query,  TableMap[] TableMappings)
{
    //Mysql vars
    MySqlConnection con;
    MySqlCommand com;
    MySqlDataAdapter adapter;
    DataSet ds;

    con = new MySqlConnection();
    con.ConnectionString = _serveraddress;

    con.Open();

    com = con.CreateCommand();
    com.CommandType = System.Data.CommandType.Text;

    foreach (string st in Query)
    {
        com.CommandText = com.CommandText + st;
    }

    com.CommandTimeout = int.MaxValue;

    adapter = new MySqlDataAdapter(com.CommandText, con);

    foreach (TableMap tm in TableMappings)
    {
        adapter.TableMappings.Add(tm.SourceTable, tm.TableSet);
    }

    ds = new DataSet();
    adapter.Fill(ds);

    return ds;
}

【问题讨论】:

  • 请显示完整的代码,从实例化dbaccess 后面的内容开始,直到打开连接。
  • 如果将int.MaxValue 更改为5000 会发生什么?
  • @SimonPrice 同样的错误:(
  • @Seteve 对不起这张照片。我试图不显示错误消息,而是显示执行时间。
  • 这不是你的代码。 dbaccess 在您的编辑中不存在。

标签: c# mysql timeout


【解决方案1】:

你的问题出在这一行

 adapter = new MySqlDataAdapter(com.CommandText, con);

在这里,您将命令文本传递给新的 MySqlDataAdapter。当然,这个 MySqlDataAdapter 不知道您在命令上设置的超时,因为两者(适配器和命令)没有链接在一起。

如果换成

 adapter = new MySqlDataAdapter(com);

然后您的适配器将使用 MySqlCommand 及其 CommandTimeout 属性

话虽如此,我真的建议您开始遵循完善的模式来处理数据库代码

public DataSet ExecuteQuery(string[] Query,  TableMap[] TableMappings)
{
    // using statement will ensure proper closing and DISPOSING of the objects
    using(MySqlConnection con = new MySqlConnection(_serveraddress))
    using(MySqlCommand com = con.CreateCommand())
    {
        con.Open();
        // Not needed, it is the default
        // com.CommandType = System.Data.CommandType.Text;

        foreach (string st in Query)
        {
            // Are you sure the st is properly terminated with a semicolon?
            com.CommandText = com.CommandText + st;
        }
        com.CommandTimeout = int.MaxValue;
        using(MySqlDataAdapter adapter = new MySqlDataAdapter(com))
        {
            foreach (TableMap tm in TableMappings)
               adapter.TableMappings.Add(tm.SourceTable, tm.TableSet);

            DataSet ds = new DataSet();
            adapter.Fill(ds);
            return ds;
       }
    }
}

using 语句在这里非常重要,特别是对于 MySql,它对未正确关闭的连接非常敏感,并且可能会停止您的程序并显示有关 没有更多可用连接的错误消息。

【讨论】:

    【解决方案2】:

    Pwd 更改为password。属性 server 也应该设置为一个 ip 或域。

    conn = "Server=[ip/domain];...password=mypassword";
    

    错误的凭据会导致登录失败,而不是超时。我猜它真的试图连接到mysql。如果服务器在您的本地计算机上运行,​​只需将 localhost 设置为服务器即可。

    【讨论】:

    • @Steve 谢谢。有用!我接受了您对“使用”命令的建议。我会修改 mysql 调用的其余部分以使用“使用”。再次感谢您
    • server 参数中,我使用的是计算机名称,因为这是在内部网络或VPN 上使用的。我应该使用内部域名的 FQDN 吗?你在这里有什么推荐?我愿意改进
    • 这在很大程度上取决于您的软件。在本地网络上,我想其中一个没有什么大好处。我通常会提供设置来更改用户端的 db-connection 表单,这样这个问题就会消失。我唯一可以肯定的是,即使 ip 发生变化,也宁愿使用 ip 的表示形式,如 FQDN 或计算机名称(从未尝试过)以保持连接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2011-03-29
    相关资源
    最近更新 更多