首页 > 作文

[PHP] PHP PDO与mysql的连接单例防止超时情况处理

更新时间:2023-04-07 21:38:12 阅读: 评论:0

这个数据库类主要处理了单例模式下创建数据库对象时,如果有两次较长时间的间隔去执行sql操作,再次处理会出现连接失败的问题,利用一个cache数组存放pdo对象与时间戳,把两次执行之间的时间进行了比较,如果间隔超过了10秒就再次new pdo创建连接,没有超过的情况下会继续使用原来的连接,并且广州医学院因为每次使用后会使连接续期,cache数组里的时间戳也进行了续期.
每次执行操作都会从cache数组中获取下连接,多次执行不超过10秒的情况下,只会有一个连接

代码中实现读写分离,判断sql语句前面6个字符是lect的就查询从库,其余操作查询主库.主库和从库就是分别在配置数组中0和1创建不同的pdo对象连接

代码如下:

<?phpclass sinapdoadapter{    const master    = 0;    const slave     = 1;    const default_cache_expiretime = 10;    private static $options 奶奶作文= array(        pdo::attr_autocommit            => true,        pdo::attr_errmode               => pdo::errmode_exception,        pdo::attr_default_fetch_mode    上海三校生高考=> pdo::fetch_assoc,        //pdo::attr_persistent            => true,    );    private $dsn = null;    private $urname = null;    private $p双拥工作assword = null;    private $timeout = null;    private $chart = null;    private $conns = array();    private $conn = null;    private $stmt = null;    private static $obj=null;    private function __construct($dsn, $urname, $password, $timeout = null, $chart = null){        $this->dsn = $dsn;        if (!is_array($urname)) {            $this->urname = array($urname);        } el {            $this->urname = $urname;        }        if (!is_array($password)) {            $this->password = array($password);        } el {            $this->password = $password;        }        $this->timeout = intval($timeout);        $this->chart = $chart;    }    private function getconnection($id = lf::master){        if (!ist($this->dsn[$id])) {            $id = lf::master;        }        $conn = $this->getcachedconn($id);        if ($conn) {            return $conn;        }        $opts = lf::$options;        if ($this->timeout > 0) {            $opts[pdo::attr_timeout] = $this->timeout;        }        $urname = ist($this->urname[$id]) ? $this->urname[$id] : $this->urname[lf::master];        $password = ist($this->password[$id]) ? $this->password[$id] : $this->password[lf::master];        $conn = new pdo($this->dsn[$id], $urname, $password, $opts);        $this->cacheconn($id, $conn);        if ($this->chart) {            $conn->exec('t names ' . $this->chart);        }        return $conn;    }    public function execute($sql, $params = array()){        $cmd = substr($sql, 0, 6);        if (strcacmp($cmd, 'lect') === 0) {            $conn = $this->getconnection(lf::slave);        } el {            $conn = $this->getconnection(lf::master);        }        $stmt = $conn->prepare($sql);        $stmt->execute($params);        $this->stmt = $stmt;        $this->conn = $conn;    }    public function fetch(){        return $this->stmt->fetch();    }    public function fetchall(){        return $this->stmt->fetchall();    }    public function lastinrtid(){        return $this->conn->lastinrtid();    }    public function rowcount(){        return $this->stmt->rowcount();    }    public static function getinstance($conf){        if(lf::$obj == null){            lf::$obj = new lf($conf->dsn,$conf->urname,$conf->password,$conf->timeout,$conf->chart);        }        return lf::$obj;    }    private function getcachedconn($id){        if (!ist($this->conns[$id])) {            return null;        }        list($conn, $timeout) = $this->conns[$id];        if (time() < $timeout) {            $this->cacheconn($id, $conn);            return $conn;        } el {            return null;        }    }    private function cacheconn($id, $conn){        $timeout = time();        if ($this->timeout) {            $timeout += $this->timeout;        } el {            $timeout += lf::default_cache_expiretime;        }        $this->conns[$id] = array($conn, $timeout);    }}$config=new stdclass();$config->dsn=array("mysql:host=127.0.0.1;port=3306;dbname=surframe",//主库"mysql:host=127.0.0.2;port=3306;dbname=surframe"//从库);$config->urname=array('root', 'root',);$config->password=array('taoshihan1', 'taoshihan1',);$config->timeout=10;$config->chart="utf8";$db=sinapdoadapter::getinstance($config);$db->execute("lect * from admin_urs");//使用的从库$rows=$db->fetchall();var_dump($学好数理化db);$db=sinapdoadapter::getinstance($config);$db->execute("lect * from admin_urs");//使用的从库$rows=$db->fetchall();var_dump($db);

本文发布于:2023-04-07 21:38:10,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/3031683bcfa4900a6ea865d4e68ad191.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:[PHP] PHP PDO与mysql的连接单例防止超时情况处理.doc

本文 PDF 下载地址:[PHP] PHP PDO与mysql的连接单例防止超时情况处理.pdf

标签:时间   两次   对象   操作
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图