diff --git a/t3lib/class.t3lib_db.php b/t3lib/class.t3lib_db.php index da539c3..a98a1f8 100644 --- a/t3lib/class.t3lib_db.php +++ b/t3lib/class.t3lib_db.php @@ -63,7 +63,8 @@ class t3lib_DB { var $explainOutput = 0; // Set this to 1 to get queries explained (devIPmask must match). Set the value to 2 to the same but disregarding the devIPmask. There is an alternative option to enable explain output in the admin panel under "TypoScript", which will produce much nicer output, but only works in FE. // Default link identifier: - var $link = FALSE; + var $link = FALSE; + var $slaveLink = FALSE; // Default character set, applies unless character set or collation are explicitely set var $default_charset = 'utf8'; @@ -78,6 +79,65 @@ class t3lib_DB { */ protected $postProcessHookObjects = array(); + + + function getMysqlLink() + { + + //$rand = rand(0, (count($GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'])+1)); + $rand = rand(0, count($GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler']) ); + + if ($rand > 0) + { + return $this->createMysqlLink( (--$rand) ); + } + else + { + return $this->link; + } + } + + + + function createMysqlLink($nr = 0) { + if (file_exists( $_SERVER["DOCUMENT_ROOT"] . "/" . $GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'][$nr]['dead_file'])) { + return $this->link; + } + + + // Slave deaktivieren: + if (@$GLOBALS['USE_MASTER_ONLY'] === TRUE) { + return $this->link; + } + + if (isset($this->slaveLink[$nr])) + return $this->slaveLink[$nr]; + $this->slaveLink[$nr] = @mysql_connect( $GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'][$nr]['typo3_db_host'], $GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'][$nr]['typo3_db_username'], $GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'][$nr]['typo3_db_password']); + if (!$this->slaveLink[$nr]) + { + /* create dead lock file */ + if (isset($GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'][$nr]['dead_file']) && $GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'][$nr]['dead_file'] != '') + { + $file = fopen($_SERVER["DOCUMENT_ROOT"] . "/" . $GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'][$nr]['dead_file'], "w"); + fputs($file, mysql_error()); + fclose($file); + } + return $this->link; + } + mysql_select_db($GLOBALS["TYPO3_CONF_VARS"]['SYS']['slave_db_handler'][$nr]['typo3_db'] , $this->slaveLink[$nr]); + $setDBinit = t3lib_div::trimExplode(LF, str_replace("' . LF . '", LF, $GLOBALS['TYPO3_CONF_VARS']['SYS']['setDBinit']), TRUE); + foreach ($setDBinit as $v) { + if (mysql_query($v, $this->slaveLink[$nr]) === FALSE) { + t3lib_div::sysLog('Could not initialize DB connection with query "' . $v . + '": ' . mysql_error($this->slaveLink[$nr]), + 'Core', + 3 + ); + } + } + return $this->slaveLink[$nr]; + } + /************************************ * @@ -185,7 +245,7 @@ class t3lib_DB { */ function exec_SELECTquery($select_fields, $from_table, $where_clause, $groupBy = '', $orderBy = '', $limit = '') { $query = $this->SELECTquery($select_fields, $from_table, $where_clause, $groupBy, $orderBy, $limit); - $res = mysql_query($query, $this->link); + $res = mysql_query($query, $this->getMysqlLink()); if ($this->debugOutput) { $this->debug('exec_SELECTquery');