View Single Post
Old 08-07-2008, 09:53 AM   #3 (permalink)
sketchMedia
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

I'm not quite sure why aggregation per se would be the cause of this, but bad design would.
If your system is managing to open multiple database connections per execution there is defiantly something wrong.

A singleton DB connection class would combat this somewhat:
PHP Code:
<?php
class DBConn
{
    private static 
$pDB;
    const 
HOST 'localhost';
    const 
USER 'root';
    const 
PASS '';
    
/*
     * Creates a single mysql connection
     */
    
public static function connect()
    {
        if(!
is_resource(self::$pDB))
        {
            
self::$pDB mysql_connect(self::HOSTself::USERself::PASS
                or 
self::throwException('Cannot connect to the database');
        }
        return 
self::$pDB;
    }
    
/*
     * Added so that i can throw exceptions using 'or' on function calls
     * I.E. mysql_connect([params]) or self::throwException([message])
     */
    
public static function throwException($szMsg)
    {
        throw new 
Exception($szMsg);
    }
    
/*
     * cannot create new object from a static class
     */
    
private function __construct() {}
    private function 
__clone() {}
}
To show it in action (sort of):
PHP Code:
class test
{
    private 
$pDb;
    
    public function 
__construct()
    {
        
$this->pDb DBConn::connect();
    }
}
try{
    
$lol = new test();
}catch(
Exception $e){
    echo 
$e->getMessage();

also just a note, mysql_connect will reuse an existing connection if possible (if the same args are passed in, according to the manual) so this method may be on the surface unnecessary, for mysql at least.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote