mysql connection pooling per script execution
Date: 01/28/07
(PHP Community) Keywords: php, mysql, asp, sql
I try not to be authoritive on anything until I got documentation to back me up and at the last minute of work on Friday I ran into a problem. One of the core programmers in my team is a recovering .net/asp developer who is still getting upto speed on PHP. He's got the block and tackle idea's but of course you can't become a master at a new language until you've had atleast one or two nervous breakdowns because the new language isn't doing what the other languages did. thedailywtf.com also helps.
Anyway: to the point. My understanding of the mysql library in PHP core is that it tries it's best to recycle instances to the server between different instances. So if script A is run by 10+ different people, php mysql is going to try and share. Apparently this is not true in .net (version unknown). My coworker got called in to fix a wtf script that would slowly devour all available connections to the DB because the last developer didn't close the connections. So he wrote this really amazing and really elaborate wrapper class that I don't think is necessary but I am not completely sure.
Basically, on wrapper creation, it checks a flag for "in_use" and if true, creates a new mysql_connect resource handle. This scares me because if I really understand it... the wrapper could create a dozen handles to the DB in one cycle of script execution then multiply that by number of individual script executions and this could be bad? Or am I wrong. Is the php mysql_ smart enough not to shoot us in the foot?
The original plan was for the DB wrapper to be used like so.
FrameworkObject->getDb("db name");
this then skims through an array structured like:
$dbc['db name'] = array('info'=>'This is the general db connection', 'server'=>'my.server.com', 'account'=>'myUserName','password'=>'pw','flags'=>NULL,'INT'=>NULL);
The key is the $dbc['db name']['INT'] which would hold the resource handle to the wrapper. Voila, instant conservation of resource handles. Then the trick is just to put in the DB::__destruct() a mysql_close();
Or am I wrong as well? or are we both wrong in ways not understood just yet?
Source: http://community.livejournal.com/php/533911.html