My application uses pcntl_fork to fork a childprocess which monitors a connected router. So I instantiate the class ConnectionManager which forks themself into as many childprocesses as machines are needed to be monitored.
I'm using a external DB class which should implement the Singleton pattern but PHP tends to build up a new Database Connection for each forked childprocess.
<?php
/**
* Database
* implements Singleton Pattern
* Returns one static object via Get()
*/
include_once '../config/config.php';
class Database{
private $log;
public static $dbLink;
/**
* returns the reference to the only possible mysql resource
* @return unknown
*/
public static function Get(){
if(!self::$dbLink){
$log = Logger::getLogger(__CLASS__);
self::$dbLink = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_SELECT);
if(!self::$dbLink){
$log->fatal("Connect to Database failed");
throw new Exception("Database connection failed: ".mysql_error());
}
}
return self::$dbLink;
}
public function Destroy(){
self::$dbLink = null;
}
}
?>
The Problem arises as the number of monitored router increases. Currently we are monitoring around 56 machines, so the application establishes 56 unique mysql_connections to the underlying database. But if the number of machines monitored arises our approach would collide with the mysql_connection_limit.
The Database is used to store the traffic of each router. Additionally each childprocess stores runtime-data which contains e.g. the last retrieve of traffic data or the allowance to run.
My question is, if its possible to use just one Connection. Something like a cache which collects the SQL statements and sends them as query to the database.
But how do I design an approach like mentioned above?
If further information is needed just ask and i'll post some snippets.
As far as I know there is no way to do this in PHP (connection pooling)
You can mysql_pconnect to reuse in the next request. but this may not do what you need.
Otherwise this may be useful to you
https://github.com/junamai2000/mod_namy_pool#readme
"The mysql connection pooling module for a prefork server (like php). You can limit the number of connections by shariing connections bewteern a parent process and child processes"