简体   繁体   中英

The timeout period elapsed prior to obtaining a connection from pool

I have problem with my homework code. We must create some database with updating users, but I am getting error with these and program crash.. This is full error code and there is code..

Error: MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

I try with query.CommandTimeout = 60; but i think its useless, what do u think?

MySqlConnection connect = new 
     MySqlConnection("Server=localhost;Database=Work;Uid=root;Pwd='1234';");
connect.Open();
MySqlCommand query = new MySqlCommand(@"UPDATE user  
        SET User_Name=@User_Name,User_BankBalance=@User_BankBalance,
        User_Password=@User_Password,LottoTimer=@LottoTimer 
        WHERE User_Name='" + Escape(u.Username) + "'", connect);
using (query)
{
    query.CommandType = System.Data.CommandType.Text;
    query.Parameters.AddWithValue("@User_Name", Escape(u.Username));
    query.Parameters.AddWithValue("@User_BankBalance", u.BankBalance);
    query.Parameters.AddWithValue("@User_Password", u.Password);
    query.Parameters.AddWithValue("@LottoTimer", u.LottoTimer);
    query.Dispose();
    query.Prepare();
    query.ExecuteNonQuery();
}
connect.Close();
return;

You method should look something like this:

        string query =
            @"UPDATE user SET"
            + " User_Name=@User_Name,"
            + " User_BankBalance=@User_BankBalance,"
            + " User_Password=@User_Password,"
            + " LottoTimer=@LottoTimer"
            + " WHERE User_Name='" + Escape(u.Username) + "'";
        using( var connect = new MySqlConnection("Server=localhost;Database=Work;Uid=root;Pwd='1234';"))
        using( var query = new MySqlCommand(query, connect))
        {
            connect.Open();
            query.CommandType = System.Data.CommandType.Text;
            query.Parameters.AddWithValue("@User_Name", Escape(u.Username));
            query.Parameters.AddWithValue("@User_BankBalance", u.BankBalance);
            query.Parameters.AddWithValue("@User_Password", u.Password);
            query.Parameters.AddWithValue("@LottoTimer", u.LottoTimer);
            query.Prepare();
            query.ExecuteNonQuery();
        }
        return;

It is important to dispose the MySqlConnection, so it can be available to the pool again. The problem you have is that all connections are used in the pool, and it is waiting for one - until the timeout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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