简体   繁体   English

C#MySQL中的连接过多

[英]C# Too many connections in MySQL

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


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 

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

This is for example how i make select: 这是例如我如何选择:

MySqlDataReader msdr; 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", id);
                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;

All the examples above show the same weakness. 以上所有示例都显示出相同的弱点。 You don't use the using statement that will ensure the propert closing and disposing of the connection and other disposable objects. 您不会使用using语句来确保适当关闭和处置连接和其他一次性对象。 If one or more of your statements throw an exception, the code that close the connection is not executed and you could end with the too many connections error 如果您的一条或多条语句引发异常,则关闭连接的代码不会执行,并且您可能会因连接过多错误而结束

For example 例如

string commandLine = "SELECT * FROM Table WHERE active=1";
commandLine = commandLine.Remove(commandLine.Length - 3);
using(MySqlConnection connect = new MySqlConnection(connectionStringMySql))
using(MySqlCommand cmd = new MySqlCommand(commandLine, connect))
{
    connect.Open();
    using(MySqlDataReader msdr = cmd.ExecuteReader())
    {
        while (msdr.Read())
        {
            //Read data
        }
    }
} // Here the connection will be closed and disposed.  (and the command also)

Decrease the wait_timeout : 减少wait_timeout

[mysqld]
wait_timeout=900

Because ADO.NET use connection pooling, it will keep your connection alive even if you dispose it. 因为ADO.NET使用连接池,所以即使您处理连接,它也可以使连接保持活动状态。 The solution is to tell MySQL to drop the connections. 解决的办法是告诉MySQL断开连接。

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

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