0

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.

Ben Matheja
  • 115
  • 2
  • 2
  • 12

1 Answers1

2

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"

Maxui
  • 210
  • 1
  • 3
  • 15
  • 3
    I'd add that using mysql_ functions on a new project should be avoided. These functions are deprecated as of PHP 5.5. Instead you should use the PDO or MySQLI classes for your connection. These support persistent connections as well which may help with your project. – Mike Hancock Sep 18 '13 at 09:17
  • sadly i have no administrative access to the production system. In addition i think namy would not be compliant to our corporate it-policy. My hope was that there would be a good idea to solve that with the tools php offers itself. but if thats not the case _ i think i'm going to drop the ball on that one. If the scalability is really needed i'd port the application to another language. – Ben Matheja Sep 19 '13 at 11:28