doctrine實現自動重連mysql資料庫機制

來源:互聯網
上載者:User

這篇文章主要介紹了doctrine實現自動重連mysql資料庫機制,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

不知道大家有沒有碰到就是mysql有的時候會八小時不使用的話自動中斷連線,這樣會導致我們的請求失敗,項目訪問報錯,資料庫斷開,這個時間要是失效了,那我們該怎麼辦呢?我們使用的是doctrine-dbal,所以那我們就寫一套自動重連的機制吧!話不多bb,直接上代碼。

<?phpnamespace WsdServer\Lib\PDO;use Doctrine\Common\EventManager;use Doctrine\DBAL\Driver;use Doctrine\DBAL\Configuration;use Doctrine\DBAL\DBALException;use Doctrine\DBAL\Cache\QueryCacheProfile;use Doctrine\DBAL\Connection AS Connection;/** * A wrapper around a Doctrine\DBAL\Connection that adds features like * reconnect * */ class WsdConnection extends Connection {    const RECONNECT_MAX_TIMES = 3;  // 最多重試次數    private $reconnectRetryTimes;            public function __construct(array $params, Driver $driver, Configuration $config = null,                                EventManager $eventManager = null)    {        parent::__construct($params, $driver, $config, $eventManager);    }            /**     * executeQuery - 支援自動重連機制的封裝     *     * Executes an, optionally parametrized, SQL query.     *     * If the query is parametrized, a prepared statement is used.     * If an SQLLogger is configured, the execution is logged.     *     * @param string $query The SQL query to execute.     * @param array $params The parameters to bind to the query, if any.     * @param array $types The types the previous parameters are in.     * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.     *     * @return \Doctrine\DBAL\Driver\Statement The executed statement.     *     * @throws DBALException     * @throws \Exception     */    public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)    {        try {                         $result = parent::executeQuery($query, $params, $types, $qcp);                         $this->reconnectRetryTimes = 0;                         return $result;        } catch (DBALException $dex){                     if ( $dex->getErrorCode() == 2006 ) {                            if ($this->reconnectRetryTimes <= WsdConnection::RECONNECT_MAX_TIMES) {                                 $this->reconnectRetryTimes++;                    secho("ORM-executeQuery", "MySQL Reconnect...("                        . $this->reconnectRetryTimes . "/" . WsdConnection::RECONNECT_MAX_TIMES . ")");                                       $this->close();                                        return $this->executeQuery($query, $params, $types, $qcp);                }            }                          throw $dex;        } catch (\Exception $ex) {                      throw $ex;        }    }                /**     * executeUpdate - 支援自動重連機制的封裝     *     * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters     * and returns the number of affected rows.     *     * This method supports PDO binding types as well as DBAL mapping types.     *     * @param string $query The SQL query.     * @param array $params The query parameters.     * @param array $types The parameter types.     *     * @return integer The number of affected rows.     *     * @throws DBALException     * @throws \Exception     */    public function executeUpdate($query, array $params = array(), array $types = array())    {        try {                    $result = parent::executeUpdate($query, $params, $types);                         $this->reconnectRetryTimes = 0;                         return $result;        } catch (DBALException $dex){                    if ( $dex->getErrorCode() == 2006 ) {                            $this->reconnectRetryTimes++;                                if ($this->reconnectRetryTimes <= WsdConnection::RECONNECT_MAX_TIMES) {                    secho("ORM-executeQuery", "MySQL Reconnect...("                        . $this->reconnectRetryTimes . "/" . WsdConnection::RECONNECT_MAX_TIMES . ")");                                        $this->close();                                        parent::executeUpdate($query, $params, $types);                }            }                    throw $dex;        } catch (\Exception $ex) {                        throw $ex;        }    }}

這樣用現在的兩個方法覆蓋了它原來本身的,這樣從連機制簡簡單單的好了,
測試了一波,發現我們 kill mysql,在重新起來的時候他會自動重新串連,原因是它本身的底層有一個,$this->connect()。所以我們不用怕了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.