Can latency cause PHP mysql DB connections to build up?
I use the following common PHP code in all a site's scripts to connect to the DB:
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS) or trigger_error('Unable to connect to database');
@mysql_select_db(DB_NAME) or trigger_error('Unable to select database ' . DB_NAME);
I've had a site running with this code for close to a year, without any issues that I could see. Today however, my host experienced some 'latency' with their DB connection. After the latency period ended, I discovered that my site was unable to connect to the DB, because the max users limit was reached for mySQL.
I'm guessing the problem is, this code kicks off an attempt to connect, and since there is a latency issue, and not a connection issue, connection attempts start piling up. Then, once the latency period ended, I now had a ton of connections.
So, my question is, how do I prevent something like this from happening in the future? Is there some sort of shorter timeout period I can use with the connect function?
I realize mysql_ is being deprecated, and this should be updated to use mysqli_, however that is not the problem, I believe.
I use the following common PHP code in all a site's scripts to connect to the DB:
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS) or trigger_error('Unable to connect to database');
@mysql_select_db(DB_NAME) or trigger_error('Unable to select database ' . DB_NAME);
I've had a site running with this code for close to a year, without any issues that I could see. Today however, my host experienced some 'latency' with their DB connection. After the latency period ended, I discovered that my site was unable to connect to the DB, because the max users limit was reached for mySQL.
I'm guessing the problem is, this code kicks off an attempt to connect, and since there is a latency issue, and not a connection issue, connection attempts start piling up. Then, once the latency period ended, I now had a ton of connections.
So, my question is, how do I prevent something like this from happening in the future? Is there some sort of shorter timeout period I can use with the connect function?
I realize mysql_ is being deprecated, and this should be updated to use mysqli_, however that is not the problem, I believe.
No comments:
Post a Comment