如何ping MySQL数据库并使用PDO重新连接 [英] How do i ping the MySQL db and reconnect using PDO

查看:484
本文介绍了如何ping MySQL数据库并使用PDO重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MySQL PDO来处理数据库查询及所有操作.但是大多数时候,MySQL连接消失了.所以我正在寻找PDO,它将检查数据库连接是否存在以及是否没有退出,那么我需要连接数据库以继续执行查询.

我是MySQL pdo的新手,我不知道如何处理这种情况.如果有人对此提出建议,那会更好.

解决方案

我试图找到相同问题的解决方案,但我找到了下一个答案:

class NPDO {
    private $pdo;
    private $params;

    public function __construct() {
        $this->params = func_get_args();
        $this->init();
    }

    public function __call($name, array $args) {
        return call_user_func_array(array($this->pdo, $name), $args);
    }

    // The ping() will try to reconnect once if connection lost.
    public function ping() {
        try {
            $this->pdo->query('SELECT 1');
        } catch (PDOException $e) {
            $this->init();            // Don't catch exception here, so that re-connect fail will throw exception
        }

        return true;
    }

    private function init() {
        $class = new ReflectionClass('PDO');
        $this->pdo = $class->newInstanceArgs($this->params);
    }
}

此处的完整故事: https://terenceyim .wordpress.com/2009/01/09/add-ping-function-to-pdo/


其他人正在考虑使用 PDO :: ATTR_CONNECTION_STATUS ,但他发现:即使停止mysqld,"$db->getAttribute(PDO::ATTR_CONNECTION_STATUS)仍在继续答复"通过UNIX套接字的Localhost". /p>

I am using MySQL PDO for handling database querying and all. But most of the time, the MySQL connection is gone away. So i am looking in the PDO that will check if the db connection is exists or not and if it is not exit, then i need to connect the database to continue the query execution.

I am new to the MySQL pdo and i do not know how to handling this situation. It would be better if anybody suggest on this.

解决方案

I tried to find a solution for the same problem and I found the next answer:

class NPDO {
    private $pdo;
    private $params;

    public function __construct() {
        $this->params = func_get_args();
        $this->init();
    }

    public function __call($name, array $args) {
        return call_user_func_array(array($this->pdo, $name), $args);
    }

    // The ping() will try to reconnect once if connection lost.
    public function ping() {
        try {
            $this->pdo->query('SELECT 1');
        } catch (PDOException $e) {
            $this->init();            // Don't catch exception here, so that re-connect fail will throw exception
        }

        return true;
    }

    private function init() {
        $class = new ReflectionClass('PDO');
        $this->pdo = $class->newInstanceArgs($this->params);
    }
}

Full story here: https://terenceyim.wordpress.com/2009/01/09/adding-ping-function-to-pdo/


Somebody else was thinking to use PDO::ATTR_CONNECTION_STATUS, but he figured out that: "$db->getAttribute(PDO::ATTR_CONNECTION_STATUS) keeps replying "Localhost via UNIX socket" even after stopping mysqld".

这篇关于如何ping MySQL数据库并使用PDO重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆