2013-06-13 88 views
0

我尝试在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() +517 MySql.Data.MySqlClient.NativeDriver.Open() +702 MySql.Data.MySqlClient.Driver.Open() +245 MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) +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

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version: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 [email protected];"; 

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; 
+2

你在连接中使用using语句吗? – Steve

+0

可能您应该在MySql目录中的my.ini文件中增加max_connections变量。不要忘记在更改变量之前关闭MySql(cmd-> net stop mysql)。 – Baurzhan

回答

4

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

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

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

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

+0

尽管增加允许的连接数量有时有助于解决原因,但它确实忽略了可能存在的潜在问题。通常,问题不仅仅是需要更多的连接,而是缺少连接。取决于垃圾收集器和处理连接最终会导致同样的问题再次出现。 –

+0

@René:这正是我的观点 - 连接需要正确关闭,这正是史蒂夫所指的。我会编辑我的答案,使其更清楚一些。 – davek

+0

啊,对。 'max_connections'似乎非常突出,很容易错过,也提到了这一点。但是你的更新肯定会更加重视它。 +1 –

2

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

我会确保你关闭连接并通过在使用语句中包装MySqlConnection和命令来隐式配置。

你可以发布一些代码,告诉你如何与数据库进行通信。

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

+0

我编辑我的问题,用一些代码..... – MTA

+0

那么你的代码似乎关闭连接罚款。您需要查看打开的连接正在进行的操作。它可能是一个你忘记关闭的特定查询或其他一些保持这些打开状态的系统/进程。有没有其他应用程序使用这个MySQL实例? – DaveHogan