After some recent changes a site has started showing the max_user_connections error which is probably a sign that too many concurrent connection attempts are being to the MySQL database.
I’ve noticed that the original programmers implemented a “DB” class for managing database connections and it seems that a close is only called in the footer of each page. How could I go about improving this? Should I create a db object for each request I want to make and then close immediately after results have been retrieved?
I am not familiar with PHP but closing DB-Connections as soon as possible is always good and independent from the programming language used.
Also try to get a new DB-Connection as late as possible.
If you’re doing transactional DML then you should also release locks as soon as possible by commiting as soon as possible (but as late as necessary). Having transactions opened longer than necessary may result in other transactions waiting for it. These waiting transactions are consuming a connection each while doing nothing than waiting.
Your “DB-Class” sounds like a Utility-Class for handling such purposes.
Have a look at this question, too.