/** * Connects to the database. * * @return void * @throw eZClusterHandlerDBNoConnectionException * @throw eZClusterHandlerDBNoDatabaseException */ public function _connect() { $siteINI = eZINI::instance('site.ini'); // DB Connection setup // This part is not actually required since _connect will only be called // once, but it is useful to run the unit tests. So be it. // @todo refactor this using eZINI::setVariable in unit tests if (self::$dbparams === null) { $fileINI = eZINI::instance('file.ini'); self::$dbparams = array(); self::$dbparams['host'] = $fileINI->variable('eZDFSClusteringSettings', 'DBHost'); self::$dbparams['port'] = $fileINI->variable('eZDFSClusteringSettings', 'DBPort'); self::$dbparams['socket'] = $fileINI->variable('eZDFSClusteringSettings', 'DBSocket'); self::$dbparams['dbname'] = $fileINI->variable('eZDFSClusteringSettings', 'DBName'); self::$dbparams['user'] = $fileINI->variable('eZDFSClusteringSettings', 'DBUser'); self::$dbparams['pass'] = $fileINI->variable('eZDFSClusteringSettings', 'DBPassword'); self::$dbparams['max_connect_tries'] = $fileINI->variable('eZDFSClusteringSettings', 'DBConnectRetries'); self::$dbparams['max_execute_tries'] = $fileINI->variable('eZDFSClusteringSettings', 'DBExecuteRetries'); self::$dbparams['sql_output'] = $siteINI->variable("DatabaseSettings", "SQLOutput") == "enabled"; self::$dbparams['cache_generation_timeout'] = $siteINI->variable("ContentSettings", "CacheGenerationTimeout"); } $serverString = self::$dbparams['host']; if (self::$dbparams['socket']) { $serverString .= ':' . self::$dbparams['socket']; } elseif (self::$dbparams['port']) { $serverString .= ':' . self::$dbparams['port']; } $maxTries = self::$dbparams['max_connect_tries']; $tries = 0; eZDebug::accumulatorStart('mysql_cluster_connect', 'MySQL Cluster', 'Cluster database connection'); while ($tries < $maxTries) { if ($this->db = mysql_connect($serverString, self::$dbparams['user'], self::$dbparams['pass'])) { break; } ++$tries; } eZDebug::accumulatorStop('mysql_cluster_connect'); if (!$this->db) { throw new eZClusterHandlerDBNoConnectionException($serverString, self::$dbparams['user'], self::$dbparams['pass']); } if (!mysql_select_db(self::$dbparams['dbname'], $this->db)) { throw new eZClusterHandlerDBNoDatabaseException(self::$dbparams['dbname']); } // DFS setup if ($this->dfsbackend === null) { $this->dfsbackend = new eZDFSFileHandlerDFSBackend(); } $charset = trim($siteINI->variable('DatabaseSettings', 'Charset')); if ($charset === '') { $charset = eZTextCodec::internalCharset(); } if ($charset) { if (!mysql_query("SET NAMES '" . eZMySQLCharset::mapTo($charset) . "'", $this->db)) { $this->_fail("Failed to set Database charset to {$charset}."); } } }
/** * Connects to the database. * * @return void * @throw eZClusterHandlerDBNoConnectionException * @throw eZClusterHandlerDBNoDatabaseException **/ public function _connect() { // DB Connection setup // This part is not actually required since _connect will only be called // once, but it is useful to run the unit tests. So be it. // @todo refactor this using eZINI::setVariable in unit tests if (self::$dbparams === null) { $siteINI = eZINI::instance('site.ini'); $fileINI = eZINI::instance('file.ini'); self::$dbparams = array(); self::$dbparams['host'] = $fileINI->variable('eZDFSClusteringSettings', 'DBHost'); self::$dbparams['port'] = $fileINI->variable('eZDFSClusteringSettings', 'DBPort'); self::$dbparams['socket'] = $fileINI->variable('eZDFSClusteringSettings', 'DBSocket'); self::$dbparams['dbname'] = $fileINI->variable('eZDFSClusteringSettings', 'DBName'); self::$dbparams['user'] = $fileINI->variable('eZDFSClusteringSettings', 'DBUser'); self::$dbparams['pass'] = $fileINI->variable('eZDFSClusteringSettings', 'DBPassword'); self::$dbparams['max_connect_tries'] = $fileINI->variable('eZDFSClusteringSettings', 'DBConnectRetries'); self::$dbparams['max_execute_tries'] = $fileINI->variable('eZDFSClusteringSettings', 'DBExecuteRetries'); self::$dbparams['sql_output'] = $siteINI->variable("DatabaseSettings", "SQLOutput") == "enabled"; self::$dbparams['cache_generation_timeout'] = $siteINI->variable("ContentSettings", "CacheGenerationTimeout"); } $serverString = self::$dbparams['host']; if (self::$dbparams['socket']) { $serverString .= ':' . self::$dbparams['socket']; } elseif (self::$dbparams['port']) { $serverString .= ':' . self::$dbparams['port']; } $maxTries = self::$dbparams['max_connect_tries']; $tries = 0; while ($tries < $maxTries) { if ($this->db = mysql_connect($serverString, self::$dbparams['user'], self::$dbparams['pass'])) { break; } ++$tries; } if (!$this->db) { throw new eZClusterHandlerDBNoConnectionException($serverString, self::$dbparams['user'], self::$dbparams['pass']); } if (!mysql_select_db(self::$dbparams['dbname'], $this->db)) { throw new eZClusterHandlerDBNoDatabaseException(self::$dbparams['dbname']); } // DFS setup if ($this->dfsbackend === null) { $this->dfsbackend = new eZDFSFileHandlerDFSBackend(); } }