private function init_read_connection() { if ($this->has_read_db && $this->read_connection === null) { $connect_array = $this->connect_array; $db_read_host_array = isset($this->connect_array['read_db_hosts']) ? $this->connect_array['read_db_hosts'] : array(); $db = $connect_array['db']; if ($db_read_host_array) { if (isset($connect_array['read_db_arithmetic']) && $connect_array['read_db_arithmetic'] == 'roll') { //轮询算法 $host = cls_rollrand::get_db_host_roll($db_read_host_array, $db); } else { $host = cls_rollrand::get_db_host_rand($db_read_host_array, $db); //随机 } if (empty($host)) { //如果从库的host不存在,去主库中查找对应的host $db_host_array = isset($this->connect_array['db_hosts']) ? $this->connect_array['db_hosts'] : array(); if ($db_host_array) { $host = $db_host_array[$db]; if (stripos($host, ',')) { //双master $host = cls_rollrand::get_write_db_host_rand($host); } } else { $host = $connect_array['host']; } } } $this->read_connection = new mysqli($host, $connect_array['user_name'], $connect_array['pass_word'], $connect_array['db'], $connect_array['port']); if ($this->read_connection->error) { echo 'Database Connect Error : ' . $this->read_connection->error; } else { $this->read_connection->query("SET NAMES 'utf8'"); } } }
private function getMasterConnection() { if (!$this->connection) { $connect_array = $this->connect_array; $db_host_array = isset($connect_array['db_hosts']) ? $connect_array['db_hosts'] : array(); if ($db_host_array) { $db = $connect_array['db']; $host = $db_host_array[$db]; if (stripos($host, ',')) { //双master $host = cls_rollrand::get_write_db_host_rand($host); } } else { $host = $connect_array['host']; } $connect_array['host'] = $host; return $this->connection = $this->getDbConnection($connect_array); } return $this->connection; }