PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

An odd bug

Note: This article was originally published at Planet PHP on 11 September 2012.
Planet PHP

I just fixed an odd bug on the WonderProxy admin pages. Occasionally there would be an issue where the administrative dashboard would refuse to load. It would display an all too common message that the database was out of connections. This was quite odd as there's generally almost no users on our site, so there's no reason for MySQL to run out of connections. I had much larger issues to work on, and the issue was almost always temporary, so I didn't really worry about it.

Last night while improving our admin dashboard I tweaked a few things, and was wondering about performance. Curious why the page was loading slowly I ran show processlist; from the MySQL console, and was astonished to see well over a hundred connections.

A few minutes of digging revealed that while the administrative dashboard was creating instances of the Contract object to display their traffic details, each Contract instance was also generating its own connection to the database! As the number of active contracts we've managed has increased over time, so has the number of connections to the database. The issue was intermittent, as the number of connections we're allowing to MySQL still exceeds the number of contracts WonderProxy has. However hourly cron jobs handling billing, maintenance, etc. can create enough connections to put it over the edge.

I've corrected my code to properly share the database connection, the page loads faster, and doesn't crash if something else is happening on the server. I don't often make such fundamental mistakes (or so I'd like to think) so I thought I would share.