I would highly suggest you keep the database connection lazy-loaded if
you either a) have a few routes that definitely don't do anything with
the database, or b) you use caching during some routes thus saving the
trip to the database.
For sites that might have many requests or a high amount of traffic, it
makes alot of sense to only connect to the database when you really need
to so that you don't (for example with mysql), hit the maximum number of
connections to the database at any one time. Remember, if you
automatically open the connection to the database at the beginning of
every request, it will general remain open for the entire request, and
every request you get will virtually be another connection open to mysql.
-ralph
Hector Virgen wrote:
Another solution would be to update your error controller to test the
exception to see if it is a Zend_Db_Adapter_Exception. When the
connection can't be made, this exception is thrown, but there may be
other times when it is thrown (like a bad query).
The advantage, however, is that your DB connection will remain
lazy-loaded, so if you're using caching there may be times when you
don't need to connect to the DB at all.
--
Hector
On Tue, Jan 12, 2010 at 10:31 AM, Jurian Sluiman
<[email protected] <mailto:[email protected]>> wrote:
I have this piece of code for my custom db resource, extending the
zend db
resource:
> $db = parent::init();
> try {
> $this->_connection = $db->getConnection();
> } catch (Exception $e) {
> $this->getBootstrap()->bootstrap('log');
> Zend_Registry::get('log')->crit('Database connection not
responding');
> }
> return $db;
Now you're using the zend db resource functionality and test for a
valid db
connection. The log (already registered in the registry) is used to
generate
some output.
Regards, Jurian
--
Jurian Sluiman
CTO Soflomo V.O.F.
http://soflomo.com
On Tuesday 12 Jan 2010 19:22:39 scs wrote:
> Hello,
> How can I catch the exception and print an error message to visitiors
> when there is a database connection error?
> DB connection params are defined in the application.ini. and db
> resource is initiated in bootstrap file.
>
> something like this? :
>
> //function initDatabase () {
> try {
> $this->bootstrap('db');
> $db = $this->getResource('db');
> Zend_Registry::set('db', $db);
>
> } catch (Exception $e) {
> echo 'db error '.$e->getMessage();
> exit; // I have to put exit here in order to prevent other init
> functions running?
> }
>
> Also,
> I have to put exit(); in catch part in order to prevent the following
> init functions run. Is this the correct way?
>
> Thanks
> scs