08-07-2008, 10:53 AM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Oct 2007
Location: Manchester, UK
Posts: 472
Thanks: 26
|
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::HOST, self::USER, self::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.
__________________
|
|
|
|