简体   繁体   English

连接太多异常

[英]Too many connections Exception

I try to run SELECT on a table in MySql and i get this error: 我尝试在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

Source Error: 源错误:

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.

Stack Trace: 堆栈跟踪:

[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 [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

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

I run it on iis with .net and C#. 我在.net和C#的iis上运行它。 Any idea how i can fix this problem? 任何想法我可以解决这个问题吗?

EDIT 编辑

I add some code from my server: 我从服务器添加一些代码:

This is for example how i make 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(); 

This is how I Delete: 这就是我删除的方式:

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();

This is how I insert: 这是我插入的方式:

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;

Open up the Admin page on your (local) MySql monitor and observe to see if the number of connection grows with time. 在您的(本地)MySql监视器上打开“管理”页面,并观察连接数是否随时间增长。 If this is unexpected behaviour, then you may have unclosed connection objects in your code (see comment from Steve below your question). 如果这是意外行为,则您的代码中可能有未关闭的连接对象(请参见问题下方Steve的注释)。 If this is not unexpected ie you know that your app will generate and hold a lot of connections, then you may need to change the setting in MySQL 如果这不是意外的,即您知道您的应用程序将生成并保持大量连接,那么您可能需要在MySQL中更改设置

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

by adjusting max_connections : 通过调整max_connections

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

UPDATE: if you don't expect and don't need so many open connections, and want to make sure you haven't overlooked any close() statements, then good practice is to employ the using construction around objects implementing IDisposable , such as connections, to make sure that they are closed as soon as possible, allowing the garbage collector to clean up objects no longer in use. 更新:如果您不希望并且不需要那么多的开放连接,并且想要确保您没有忽略任何close()语句,那么好的做法是在实现IDisposable对象周围using结构,例如连接,以确保尽快关闭它们,从而使垃圾收集器可以清理不再使用的对象。

I don't think adjusting max connections is the right solution until it's been identified what the problem is. 在确定问题所在之前,我不认为调整最大连接数是正确的解决方案。 It could add more strain on the server / hide the actual issue. 这可能会增加服务器的负担/隐藏实际问题。

I would make sure you're closing connections and disposing implicitly by wrapping MySqlConnection and commands in using statements. 我将通过在using语句中包装MySqlConnection和命令来确保关闭连接并隐式处理。

Can you post up some code as to how you're communicating with the DB. 您是否可以发布一些有关如何与数据库通信的代码。

Also is this server a local instance? 该服务器也是本地实例吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM