Re: do pear packages create their own seperate DB connections?
| From: | Wolfram Kriesing | Date: | Tue, 22 Apr 2003 14:13:04 +0000 |
| Subject: | Re: do pear packages create their own seperate DB connections? | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-15329@lists.php.net to get a copy of this message | ||
is it not the case, that PEAR::DB (or mysql) currently uses the same instance (or at least connection) for the same DSN?
That might also be the better choice, to let the package you use decide if it needs a DB-connection.
Imagine the case you have an application which does some caching and is a bit more complex. It might be possible that some pages get shown which don't need a DB-connection, since they come completely from the cache. In this case it would be overhead to make a DB-connection every time in your config.php (or whatever it's called). You would always make a DB connection also if it is not needed.
But if you pass the task on to the package (Auth, Tree, DB_QueryTool, etc.), which knows if it has to connect to the DB or not. It might does not connect in case that it only returns a cached result or reads data only from the session (or any other case). But if the package really needs to read something from the DB it calls [M]DB::connect(DSN) and the DB-package decides if a connection exists and if it can be reused. This way you never have to worry about setting up a db-connection and passing on a global variable which contains the DB-instance and you dont have to worry about proper referencing it etc. In case you define your DB_DSN as a constant it is the best and easiest case IMO. Since you can use the constant anywhere and you dont have to define and $db as global, or whatever.
This is just the experience I gained over the last years.
just my 2 cents
Wolfram
Lukas Smith wrote:
-- Wolfram ... opensource @ vision:produktion ... http://opensource.visionp.de ... translating template engine .... http://sf.net/projects/simpletpl ... authentication system .... http://sf.net/projects/authFrom: Jeroen Houben [mailto:jeroen@terena.nl] Sent: Tuesday, April 22, 2003 3:09 PMA lot of packages use DB (and in the future hopefully MDB) and theyallneed a DSN as a constructor parameter. IMO it would be nice ifpackageslike Auth and Tree etc etc would also accept a reference to anexistingconnection. Just to give you an example of how things can look right now (syntaxmaynot be completely correct) $conn = MDB::connect($dsn); $auth = new Auth('imap',array("dsn"=>$dsn)); $tree = Tree::create('db', array("dsn"=>$dsn)); $pm = new Auth_prefemanager(array("dsn"=>$dsn)); These would all create seperate links and thus overhead, right? Ordoes(M)DB already check for an existing connection and use that if it can? Jeroengood idea. The MDB_Manager has the possibility of getting an instance of MDB instead of a dsn string or array. Generally with php4 the annoying bit is that this has to be pass by reference which makes it in turn impossible to make these parameters optional etc. But that should not be a big deal as I assume these packages will always needs a dsn otherwise anyways. Aside form that the only method to handle the needless overhead a bit if presistant connections. But it doesn't get rid of object instance creation. Also if you use only one MDB instance you can make much better use of its internal debugging mechanism because then you really see which queries were made in what order (I bet nobody uses the debugging functionality today .. if you are interested look in MDB/tests/MDB_test.php towards the bottom) Regards, Lukas