protected function getNewConnection($group, $node, $role) { $hostTotal = count($this->servers[$group][$node][$role]); $hostIndexArray = array_keys($this->servers[$group][$node][$role]); while ($hostTotal) { $hashNumber = substr(microtime(), 7, 1) % $hostTotal; $hostConfig = $this->servers[$group][$node][$role][$hostIndexArray[$hashNumber]]; $dbFactory = new LtDbAdapterFactory(); $this->connectionAdapter = $dbFactory->getConnectionAdapter($hostConfig["connection_adapter"]); $this->sqlAdapter = $dbFactory->getSqlAdapter($hostConfig["sql_adapter"]); if ($connection = $this->connectionAdapter->connect($hostConfig)) { $this->connectionAdapter->exec($this->sqlAdapter->setSchema($hostConfig["schema"]), $connection); $this->connectionAdapter->exec($this->sqlAdapter->setCharset($hostConfig["charset"]), $connection); $this->saveConnection($hostConfig, $connection, $hostConfig["connection_ttl"]); return $connection; } else { //trigger_error('connection fail', E_USER_WARNING); //delete the unavailable server for ($i = $hashNumber; $i < $hostTotal - 1; $i++) { $hostIndexArray[$i] = $hostIndexArray[$i + 1]; } unset($hostIndexArray[$hostTotal - 1]); $hostTotal--; } //end else } //end while return false; }
protected function getCurrentSqlAdapter() { $factory = new LtDbAdapterFactory(); $host = key($this->servers[$this->group][$this->node][$this->role]); return $factory->getSqlAdapter($this->servers[$this->group][$this->node][$this->role][$host]["sql_adapter"]); }