php 可重连的PDO
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 可重连的PDO相关的知识,希望对你有一定的参考价值。
<?php
class ReconnectingPDO
{
protected $dsn, $username, $password, $pdo, $driver_options;
public function __construct($dsn, $username = "", $password = "", $driver_options = array())
{
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
$this->driver_options = $driver_options;
}
public function __call($name, array $arguments)
{
try {
$this->connection()->query("SHOW STATUS;")->execute();
} catch(\PDOException $e) {
if($e->getCode() != 'HY000' || !stristr($e->getMessage(), 'server has gone away')) {
throw $e;
}
$this->reconnect();
}
return call_user_func_array(array($this->connection(), $name), $arguments);
}
protected function connection()
{
return $this->pdo instanceof \PDO ? $this->pdo : $this->connect();
}
public function connect()
{
$this->pdo = new PDO($this->dsn, $this->username, $this->password, (array) $this->driver_options);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->pdo;
}
public function reconnect()
{
$this->pdo = null;
return $this->connect();
}
}
以上是关于php 可重连的PDO的主要内容,如果未能解决你的问题,请参考以下文章
关于网页游戏断线重连的思路和demo求助
RTSP连接中断重连的问题
(接上一条)解决ssh隧道断开自动重连的问题
netty的一个小Demo-有ack和重连的功能
用python编写与mysql数据库连接并实现断线重连的问题
MFC用ADO方式连接数据库,断线重连的问题,如何恢复数据库链接