Re: do pear packages create their own seperate DB connections?

From: Date: Tue, 22 Apr 2003 15:11:57 +0000
Subject: Re: do pear packages create their own seperate DB connections?
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-15334@lists.php.net to get a copy of this message
Jeroen Houben wrote:
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.
By package you mean (M)DB or packages like Tree and Auth etc... ?
the latter
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.
Sounds good. I also use constants for my DSN because they never need to change during a script (that I know of) and they can be used everywhere. So what you're saying is the packages that need an an (M)DB connection (Tree, Auth, prefemanager...) should be responsible for finding out if there is a connection for them to use and *not* the user creating the object. That sounds simpler indeed, because you don't have to chose whether to pass a connection reference or a DSN. Example just so see if I get exactly what you mean: $MyPackage = MyPackage(DB_DSN); inside MyPackage it will do: if (existsDbConn) { // use that conn } else { // create new conn using the DSN }
what i mean: i used to do it this way: $db = DB::connect(DB_DSN); class myTree extends Tree_Whatever {
    function myTree()
    {
        global $db;
        parent::Tree_Whatever($db);
    }
} now i (try to) do class myTree extends Tree_Whatever {
    function myTree()
    {
        parent::Tree_Whatever(DB_DSN);
    }
} now i dont have to instanciate the DB myself, since i dont really need it. Only the packages i use need the DB-connection. I actually use only 3 packages that need a DB-connection (iirc) Auth, Tree, DB_QueryTool. To those packages i simply pass the DB_DSN and they (should) handle themselves if/when they need a DB-connection. The Auth package (http://sf.net/projects/auth) I use does only need a DB-connection when you log in (iirc) otherwise it always does only read/write to/from the session. But to try to answer your question, I think what you want to do might be: if (do i need a DB-connection?) {
    $db = DB::connect();
    do DB stuff
} else {
    get data/results or do action without
    the need of using db-data
} i hope i could explain what i mean :-) -- Wolfram ... opensource @ vision:produktion ... http://opensource.visionp.de ... translating template engine .... http://sf.net/projects/simpletpl ... authentication system .... http://sf.net/projects/auth

« previous php.pear.dev (#15334) next »