连接过多异常

本文关键字:异常 连接 | 更新日期: 2023-09-27 18:18:15

我试着在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): Too many connections]MySql.Data.MySqlClient.MySqlStream.ReadPacket () + 517MySql.Data.MySqlClient.NativeDriver.Open () + 702MySql.Data.MySqlClient.Driver.Open () + 245MySql.Data.MySqlClient.Driver。创建(MySqlConnectionStringBuilder设置)+297MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () + 18MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () + 403MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () + 228MySql.Data.MySqlClient.MySqlPool.GetConnection () + 106MySql.Data.MySqlClient.MySqlConnection.Open () + 1468

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

我用。net和c#在iis上运行它。有什么办法可以解决这个问题吗?

<标题>编辑

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

下面是我如何设置select:

的示例
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;

连接过多异常

打开(本地)MySql监视器上的Admin页面,观察连接数是否随时间增长。如果这是意想不到的行为,那么你的代码中可能有未关闭的连接对象(参见Steve在你的问题下面的评论)。如果不是意想不到的,即你知道你的应用程序将生成并保持大量连接,那么你可能需要更改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

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

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

我将确保你关闭连接和隐式处理通过包装MySqlConnection和命令在using语句。

你可以张贴一些代码,你是如何与数据库通信的。

该服务器是否为本地实例?