in my zf application i have base model class Application_Model, which connects to db and stores connection in registry. part of code:
$db = Zend_Db::factory($registry['config']->$databaseAlias->db->adapter, $registry['config']->$databaseAlias->db->config->toArray());
$db -> query('SET NAMES ' . $registry['config']->$databaseAlias->db->config->setnames);
$db -> setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('db', array($databaseAlias=>$db));
if mysql server is shut down, $db -> query('SET NAMES ' . $registry['config']->$databaseAlias->db->config->setnames);
throws exception. that's not what i want.
actualy i want to check somehow, if my database dead - do some actions;
is there any way to do so?
You could throw a try/catch around the db call, and if it gets into the catch, do the actions you want
try
{
$db = Zend_Db::factory(
$registry['config']->$databaseAlias->db->adapter,
$registry['config']->$databaseAlias->db->config->toArray()
);
$db -> query('SET NAMES ' . $registry['config']->$databaseAlias->db->config->setnames);
$db -> setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('db', array($databaseAlias=>$db));
} catch( \Exception $e ) {
// Database query failed, do logic here
}