/** * 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_SLAVE, 'dump'); } catch (Exception $e) { throw new MWException(__METHOD__ . " rotating DB failed to obtain new database (" . $e->getMessage() . ")"); } }