/** * 返回一个数据库对象,该服务器为读数据准备 * * @return MooMySQL */ public function getReadConnection() { // 只有一个服务器,则读写为同一个服务器 if ($this->total_read_count < 1) { return $this->getWriteConnection(); } // 如果当前的连接没有关闭,则使用当前的连接 if (!is_null(self::$read_conn_pool[self::$current_index])) { return self::$read_conn_pool[self::$current_index]; } // 取下一个读的服务器 ++self::$current_index; if (self::$current_index > $this->total_read_count - 1) { self::$current_index = 0; } // 如果所需的连接没有打开,则打开该连接 if (!isset(self::$read_conn_pool[self::$current_index]) || is_null(self::$read_conn_pool[self::$current_index])) { self::$read_conn_pool[self::$current_index] = $this->createConnection($this->read_hosts[self::$current_index]); } return self::$read_conn_pool[self::$current_index]; }