Am developing a Zend application where i need to keep a single instance of the database object and reconnect if the current instance is somehow disconnected. Here is the code:
class Resource_PdoMysql extends Zend_Application_Resource_ResourceAbstract
{
const KEY = 'PDO_MYSQL';
private static function connect()
{
$connParams = array("host" => host,
"port" => 'port',
"username" => 'username',
"password" => 'password',
"dbname" => 'dbname');
$db = new Zend_Db_Adapter_Pdo_Mysql($connParams);
return $db;
}
public static function getConnection()
{
if (!Zend_Registry::isRegistered(self::KEY))
{
$db = self::connect();
Zend_Registry::set(self::KEY, $db);
}
return Zend_Registry::get(self::KEY);
}
public static function reconnect()
{
$db = self::connect();
Zend_Registry::set(self::KEY, $db);
}
public function init()
{
return self::getConnection();
}
}
Am using $db like this
$db = Resource_PdoMysql::getConnection();
// <Here I need to check if the connection is open before proceeding>
$db->insert('table', $data);