exceeded the 'max_user_connections' Resource

Asked

Viewed 1,157 times

0

I’m not sure what the reason for the error I’m having but it seems that it is something related to open connections in the bank, where error is occurring if the application tries to create more connections than the limit. I believe this is the reason for the mistake.

I am closing all Connection I open for an interaction (Insert/update/delete/select).

I’ve read a few topics about this here on Stack Overflow, but no C# based that solved this problem.

Follow my connection code db and an interaction as an example:

Class for Connection:

 public class ClassConexao  
    {
     public static MySqlConnection ObterConexao()
     {           
       MySqlConnection conectar = new MySqlConnection("server=ENDEREÇO; database=NOME; Uid=USER; pwd=****");

       conectar.Open();
       return conectar;           
     }  
}

Interacting :

        try
        {   //ABRINDO CONEXAO01
            MySqlConnection conexão01= ClassConexao.ObterConexao();

            MySqlCommand _comandoSel = new MySqlCommand(String.Format("SELECT Column1, Column2 FROM tableA WHERE Column1 = " + "500" + ""), conexão01);
            MySqlDataReader 01_reader = _comando01.ExecuteReader();
            Sel_reader.Read();

            textbox1.Text = 01_reader.GetString(0);
            textbox2.Text = 01_reader.GetString(1);

            //FECHANDO CONEXAO01
            conexão01.Close();

        catch (Exception error)
        {
            MessageBox.Show(String.Format("Algo está errado com a operação! {0}", error.Message));
            return;
        }

Error message returned from:

inserir a descrição da imagem aqui

Is there any way to increase this limit? Or other solution that does not limit connections?

  • Give a Dispose on the connection after Close and see if it resolves.

  • @Ricardo How do I use Dispose ? connection01.Dispose(); ?

  • Look at my answer, I believe this can solve.

2 answers

3

Assuming you’re using the Mysqlconnection then you should be able to do so:

try
   {   //ABRINDO CONEXAO01
       MySqlConnection conexão01= ClassConexao.ObterConexao();

       MySqlCommand _comandoSel = new MySqlCommand(String.Format("SELECT Column1, Column2 FROM tableA WHERE Column1 = " + "500" + ""), conexão01);
       MySqlDataReader 01_reader = _comando01.ExecuteReader();
       Sel_reader.Read();

       textbox1.Text = 01_reader.GetString(0);
       textbox2.Text = 01_reader.GetString(1);

       //FECHANDO CONEXAO01
       conexão01.Close();

       catch (Exception error)
       {
          MessageBox.Show(String.Format("Algo está errado com a operação! {0}", error.Message));
          return;
       }
        finally
       {
           conexão01.Dispose(); // << AQUI VOCÊ LIBERA TODOS OS RECURSOS USADOS
        }

So it’s more accurate, do the proper checks at each place on your system that you use.

  • If you pop an Exception halfway through the resources will not be released...

  • I’ve updated the code! Vlw! I’ve also put it to him to observe every place that will implement it. Thank you.

  • Good. Now yes, +1

  • @Ricardo It didn’t help much man, I put the Dispose() after all my Close(). but I still can’t get more than 4 or 5 people working on the system.

  • Mauritius, maybe you really have a lot of people using the system, what you can try is to improve your methods where the bank is used, like, open the bank, do the necessary at once and close, then do the rest. Sometimes there is a routine that opens the connection to the bank and sends a message halfway through, or does a lot before closing. That’s as much as I can help without seeing all the code.

1

If you want to expand the connection limit, as mentioned, the limit should be expanded directly on my.cnf, if you have access to this file.

Actually there are two parameters to be changed:

  • max_connections establishes the maximum connections that can exist simultaneously.

  • max_user_connections establishes the maximum number of simultaneous connections that can exist for each user, that is the maximum number of connection for each USER mysql.

If you have access to my.cnf simply change the parameters as you wish, be aware that the use of CPU and mainly RAM will increase as the number of simultaneous requests, that is, as much as you can define "99999999" there will be hardware limits.

Check by giving a SHOW FULL PROCESSLIST in Mysql to see which connections are open and what is being done.


Also, to not have to restart Mysql for the new parameters to be used, modify using the SET GLOBAL.

SET GLOBAL max_user_connections = 500;
SET GLOBAL max_connections = 500;

For this to work the Mysql user, which will execute this query, need global privilege. This change is not permanent, whenever Mysql shuts down will return to the value set in my.cnf, therefore amend the my.cnf it is necessary.

  • I tried to put the Dispose() as Ricardo said in the other answer, but has not yet relieved the system. I think raising that limit like you said would be the solution, but I don’t think I have access to that my.cnf there.. can increase the limit in some other way ? , the way I am 4 or 5 logged in at the same time. already exceeds 30 User_connections which is where the error !

  • 1

    Depends on the server, that’s not in the question. If being on a shared server is impossible, this limitation is usually in the terms and is "immutable". If you are a VPS/Dedicated you can change. If you are using any database offered as a service (such as Google Cloud SQL, Rackspace Cloud Database...), each type is a type, it may be that each plan can have a limit or can be adjusted in the options, including can allow changing my.cnf. Anyway it depends on where Mysql is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.