簡體   English   中英

連接太多異常

[英]Too many connections Exception

我嘗試在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;

在您的(本地)MySql監視器上打開“管理”頁面,並觀察連接數是否隨時間增長。 如果這是意外行為,則您的代碼中可能有未關閉的連接對象(請參見問題下方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/zh-CN/server-system-variables.html#sysvar_max_connections

更新:如果您不希望並且不需要那么多的開放連接,並且想要確保您沒有忽略任何close()語句,那么好的做法是在實現IDisposable對象周圍using結構,例如連接,以確保盡快關閉它們,從而使垃圾收集器可以清理不再使用的對象。

在確定問題所在之前,我不認為調整最大連接數是正確的解決方案。 這可能會增加服務器的負擔/隱藏實際問題。

我將通過在using語句中包裝MySqlConnection和命令來確保關閉連接並隱式處理。

您是否可以發布一些有關如何與數據庫通信的代碼。

該服務器也是本地實例嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM