I understand there is no connection pooling in PHP Connection pooling in PHP, and we are currently using Pear DB.
I have a legacy cron job code, which is using pear DB connection.
while (true) {
...
foreach ($keys as $key) {
$connection_string = get_connection_string_based_on_key($key);
$DB = & \DB::connect($connection_string);
...
// Avoid resource leakage.
$DB->disconnect();
}
}
We realize DB::connect
does give us some performance hotspot. I plan to make a pseudo connection pool
$pool = array();
while (true) {
...
foreach ($keys as $key) {
$connection_string = get_connection_string_based_on_key($key);
if (array_key_exists ($connection_string, $pool) {
$DB = $pool[$connection_string];
} else {
$DB = & \DB::connect($connection_string);
$pool[$connection_string] = $DB;
}
...
// No $DB->disconnect(); As we want the
// DB connection remains valid inside the pool.
}
}
The cron job might run for several days, several weeks or several months. I was wondering, is there any catcha behind such pseudo connection pool? For instance,
- Will DB connection remain valid, after it stays inside pool for a long period (Says a week)?
- Possible run out of DB resource? If yes, what is a suitable mechanism to handle ever growing pool?