這篇文章主要介紹了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()。所以我們不用怕了