MySQL "1040 - Too Many connections"

Hello,
I have a web app which connect to MySQL server. When i get many times the error “1040 - Too many connections” from MySQL then, the web app becomes unresponsive and needs to get restarted otherwise it will remain stucked.
How should i handle this error?

Thank you.

Check the MySQL systemvariable ‘maxconnections

And you can probably configure it properly.

e.g. your web app should be allowed to make a lot of connections from the IP from the Webserver.

Thank you for your replies.
Indeed an increase in max_connections variable would solve the problem.

The case is why the web app becomes unresponsive? Is this a normal “reaction”?

It probably becomes unresponsive while waiting for MySQL to connect, which it cant if you’re over quota.

Hope this may change someday, also for Desktop Apps :slight_smile:

If the web app is unresponsive because of MySQL (too many connections) and the MySQL connect again the web app will never come back. It needs restart.
Is this the way that the framework handles this error or we can do something in our code to handle this?

You may want to look into why your web app needs so many connections in the first place. Does the number of concurrent sessions really exceed maxconnections or does maybe your webapp create new connections all over the place without closing or reusing the previous ones? If you really have so many sessions you might have to use a connection pool.

This sounds like a DOS attack waiting to happen.

Are you creating a new database connection within every method? I so, I recommend creating it once then using the one connection multiple times, checking it is still alive before using it.