【问题标题】:Too many connections Exception连接过多异常
【发布时间】:2013-06-13 09:35:06
【问题描述】:

我尝试在MySql 的表上运行SELECT,但出现此错误:

Server Error in '/MyApp' Application.
Too many connections
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: MySql.Data.MySqlClient.MySqlException: Too many connections

来源错误:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

堆栈跟踪:

[MySqlException (0x80004005): 连接太多] MySql.Data.MySqlClient.MySqlStream.ReadPacket() +517 MySql.Data.MySqlClient.NativeDriver.Open() +702 MySql.Data.MySqlClient.Driver.Open() +245 MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder 设置)+297 MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() +18 MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() +403 MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() +228 MySql.Data.MySqlClient.MySqlPool.GetConnection() +106 MySql.Data.MySqlClient.MySqlConnection.Open() +1468

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1

我在带有 .net 和 C# 的 iis 上运行它。 知道如何解决这个问题吗?

编辑

我从我的服务器添加了一些代码:

这就是我如何进行选择的例子:

MySqlDataReader msdr;

MySqlConnection connect = new MySqlConnection(connectionStringMySql);
MySqlCommand cmd = new MySqlCommand();

string commandLine = "SELECT * FROM Table WHERE active=1";

commandLine = commandLine.Remove(commandLine.Length - 3);
cmd.CommandText = commandLine;

cmd.Connection = connect;
cmd.Connection.Open();

msdr = cmd.ExecuteReader();

while (msdr.Read())
{
    //Read data
}
msdr.Close();
cmd.Connection.Close(); 

这就是我删除的方式:

MySqlConnection connect = new MySqlConnection(connectionStringMySql);
MySqlCommand cmd = new MySqlCommand();

cmd.Connection = connect;
cmd.Connection.Open();

string commandLine = @"DELETE FROM Table WHERE id=@id;";

cmd.CommandText = commandLine;

cmd.Parameters.AddWithValue("@id", slotId);

cmd.ExecuteNonQuery();
cmd.Connection.Close();

这就是我插入的方式:

MySqlConnection connect = new MySqlConnection(connectionStringMySql);
MySqlCommand cmd = new MySqlCommand();

cmd.Connection = connect;
cmd.Connection.Open();

string commandLine = @"INSERT INTO Table (id, weekday, start, end) VALUES" +
    "(@ id, @weekday, @start, @end);";

cmd.CommandText = commandLine;

cmd.Parameters.AddWithValue("@ id", item.babysitterid);
cmd.Parameters.AddWithValue("@weekday", item.weekday);
cmd.Parameters.AddWithValue("@start", new TimeSpan(item.starthour, item.startmin, 0));
cmd.Parameters.AddWithValue("@end", new TimeSpan(item.endhour, item.endmin, 0));

cmd.ExecuteNonQuery();
long id = cmd.LastInsertedId;
cmd.Connection.Close();
return id;

【问题讨论】:

  • 您是否在连接周围使用 using 语句?
  • 可能你应该在 MySql 目录的 my.ini 文件中增加 max_connections 变量。在更改变量之前不要忘记关闭 MySql (cmd->net stop mysql)。

标签: c# mysql .net


【解决方案1】:

在您的(本地)MySql 监视器上打开管理页面并观察连接数是否随时间增长。如果这是意外行为,那么您的代码中可能有未关闭的连接对象(请参阅问题下方史蒂夫的评论)。如果这并非出乎意料,即您知道您的应用会生成并保持大量连接,那么您可能需要更改 MySQL 中的设置

https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html

通过调整max_connections:

https://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_max_connections

更新:如果您不期望也不需要这么多打开的连接,并且想要确保您没有忽略任何 close() 语句,那么好的做法是使用围绕实现IDisposable 的对象(例如连接)的using 构造,以确保它们尽快关闭,从而允许垃圾收集器清理不再使用的对象。

【讨论】:

  • 虽然增加允许的连接数有时有助于解决问题,但它确实忽略了一个可能的潜在问题。通常问题不仅仅是需要更多的连接,而是缺少正确关闭的连接。并且依赖于垃圾收集器和连接的处理最终会导致同样的问题再次出现。
  • @René:这正是我的观点 - 需要正确关闭连接,这正是史蒂夫所暗示的。我将编辑我的答案以使其更清晰。
  • 啊,对。 max_connections 似乎非常突出,很容易被忽略,也提到了这一点。但是您的更新肯定会更加强调它。 +1
【解决方案2】:

在确定问题所在之前,我认为调整最大连接数不是正确的解决方案。它可能会给服务器增加更多压力/隐藏实际问题。

我会确保您关闭连接并通过在 using 语句中包装 MySqlConnection 和命令来隐式处理。

您能否发布一些关于您如何与数据库通信的代码。

这台服务器也是本地实例吗?

【讨论】:

  • 好吧,您的代码似乎可以很好地关闭连接。您需要查看打开的连接在做什么。可能是您忘记关闭的特定查询或其他一些保持这些查询打开的系统/进程。是否有其他应用程序使用此 MySQL 实例?
猜你喜欢
  • 1970-01-01
  • 2021-09-28
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多