User epiz_23862135 already has more than 'max_user_connections' active connections

The issue you’re experiencing, as well as the code that’s generating it, is quite similar to a case from a month ago.

The issue is that with your code structured like this, every object instance creates it’s own database connection. And in the example, it even creates two connection for a single class! So if you instantiate, say, five different objects, this can result in 5 (10?) database connections for a single requests, which makes it very easy to hit the connection limit.

The right way to fix this is to only create one database connection per request and use that across all objects and classes. There are few different patterns you can use for this, but the Singleton Pattern (which I explained in that other post) is effective and simple to implement.

6 Likes