/** * Drop the database connection $this->db and try to get a new one. * * This function tries to get a /different/ connection if this is * possible. Hence, (if this is possible) it switches to a different * failover upon each call. * * This function resets $this->lb and closes all connections on it. * * @throws MWException */ function rotateDb() { // Cleaning up old connections if (isset($this->lb)) { $this->lb->closeAll(); unset($this->lb); } if ($this->forcedDb !== null) { $this->db = $this->forcedDb; return; } if (isset($this->db) && $this->db->isOpen()) { throw new MWException('DB is set and has not been closed by the Load Balancer'); } unset($this->db); // Trying to set up new connection. // We do /not/ retry upon failure, but delegate to encapsulating logic, to avoid // individually retrying at different layers of code. // 1. The LoadBalancer. try { $this->lb = wfGetLBFactory()->newMainLB(); } catch (Exception $e) { throw new MWException(__METHOD__ . " rotating DB failed to obtain new load balancer (" . $e->getMessage() . ")"); } // 2. The Connection, through the load balancer. try { $this->db = $this->lb->getConnection(DB_REPLICA, 'dump'); } catch (Exception $e) { throw new MWException(__METHOD__ . " rotating DB failed to obtain new database (" . $e->getMessage() . ")"); } }
function _dbtable($tblName, $dbKey = "app") { if ($dbKey === true) { $dbKey = "core"; } if (Database::isOpen($dbKey)) { return Database::dbConnection($dbKey)->get_Table($tblName); } else { return false; } }
/** * @param string $serverType * @param array $tables */ protected function buildTestDatabase($tables) { global $testOptions, $wgDBprefix, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname; $wgDBprefix = 'parsertest'; $db = new Database($wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname); if ($db->isOpen()) { if (!(strcmp($db->getServerVersion(), '4.1') < 0 and stristr($db->getSoftwareLink(), 'MySQL'))) { # Database that supports CREATE TABLE ... LIKE foreach ($tables as $tbl) { $newTableName = $db->tableName($tbl); #$tableName = $this->oldTableNames[$tbl]; $tableName = $tbl; $db->query("CREATE TEMPORARY TABLE {$newTableName} (LIKE {$tableName})"); } } else { # Hack for MySQL versions < 4.1, which don't support # "CREATE TABLE ... LIKE". Note that # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0" # would not create the indexes we need.... foreach ($tables as $tbl) { $res = $db->query("SHOW CREATE TABLE {$tbl}"); $row = $db->fetchRow($res); $create = $row[1]; $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `' . $wgDBprefix . '\\1`', $create); if ($create === $create_tmp) { # Couldn't do replacement wfDie("could not create temporary table {$tbl}"); } $db->query($create_tmp); } } return $db; } else { // Something amiss return null; } }