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

零到壹度
發布: 2023-03-23 07:20:02
原創
1787 人瀏覽過

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

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

<?php
namespace 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()。所以我們不用害怕了

以上是doctrine實作自動重連mysql資料庫機制的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!