From 764dc5b2538bf668624ab40f3ee22ab42c826ff1 Mon Sep 17 00:00:00 2001 From: Loftor Date: Sat, 12 Apr 2014 20:51:15 +0800 Subject: [PATCH] Update Db.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改数据库连接池实现 --- var/Typecho/Db.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/var/Typecho/Db.php b/var/Typecho/Db.php index 1bb00d16..89f05f45 100644 --- a/var/Typecho/Db.php +++ b/var/Typecho/Db.php @@ -188,7 +188,7 @@ class Typecho_Db public function addServer($config, $op) { $this->_config[] = Typecho_Config::factory($config); - $key = key($this->_config); + $key = count($this->_config)-1; //获取最后一个key /** 将连接放入池中 */ switch ($op) { @@ -304,16 +304,14 @@ class Typecho_Db /** Typecho_Db_Exception */ throw new Typecho_Db_Exception('Missing Database Connection'); } - - $selectConnection = rand(0, count($this->_pool[$op]) - 1); - $selectConnectionConfig = $this->_config[$selectConnection]; + + //获取相应读或写服务器连接池中的一个 + $selectConnection = rand(0, count($this->_pool[$op]) - 1); + //获取随机获得的连接池配置 + $selectConnectionConfig = $this->_config[$this->_pool[$op][$selectConnection]]; $selectConnectionHandle = $this->_adapter->connect($selectConnectionConfig); - $other = (self::READ == $op) ? self::WRITE : self::READ; - - if (!empty($this->_pool[$other]) && in_array($selectConnection, $this->_pool[$other])) { - $this->_connectedPool[$other] = &$selectConnectionHandle; - } $this->_connectedPool[$op] = &$selectConnectionHandle; + } $handle = $this->_connectedPool[$op];