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

From: Date: Tue, 22 Apr 2003 14:44:24 +0000
Subject: Re: do pear packages create their own seperate DB connections?
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-15333@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. By package you mean (M)DB or packages like Tree and Auth etc... ? > > 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. > 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 } Regards, Jeroen

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