php – 如何检查Zend_Db_Adapter_Pdo_Mysql对象是否连接到数据库

我正在开发一个Zend应用程序,我需要保留数据库对象的单个实例,并在当前实例以某种方式断开连接时重新连接.这是代码

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);

解决方法:

根据Zend_Db_Adapter documentation,您可能在调用适配器getConnection()方法后捕获异常.这是示例中的复制粘贴摘录:

try {
    $db = Zend_Db::factory('Pdo_MysqL', $parameters);
    $db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
    // perhaps a Failed login credential, or perhaps the RDBMS is not running
} catch (Zend_Exception $e) {
    // perhaps factory() Failed to load the specified Adapter class
}

希望有所帮助,

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...