The timeout period elapsed prior to obtaining a connection from pool

0

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;
c#
mysql
sql
asked on Stack Overflow Sep 5, 2019 by Djani_Bulgaria • edited Sep 5, 2019 by Steve

1 Answer

1

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.

answered on Stack Overflow Sep 5, 2019 by Frank Nielsen • edited Sep 5, 2019 by David R Tribble

User contributions licensed under CC BY-SA 3.0