Merge "database: Small DB class cleanups"
[lhc/web/wiklou.git] / includes / db / DatabaseMysqlBase.php
index 9285d70..e326909 100644 (file)
@@ -580,9 +580,12 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        function ping() {
                $ping = $this->mysqlPing();
                if ( $ping ) {
+                       // Connection was good or lost but reconnected...
+                       // @note: mysqlnd (php 5.6+) does not support this (PHP bug 52561)
                        return true;
                }
 
+               // Try a full disconnect/reconnect cycle if ping() failed
                $this->closeConnection();
                $this->mOpened = false;
                $this->mConn = false;
@@ -1052,6 +1055,28 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                        ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
        }
 
+       /**
+        * Get the underlying binding handle, mConn
+        *
+        * Makes sure that mConn is set (disconnects and ping() failure can unset it).
+        * This catches broken callers than catch and ignore disconnection exceptions.
+        * Unlike checking isOpen(), this is safe to call inside of open().
+        *
+        * @return resource|object
+        * @throws DBUnexpectedError
+        * @since 1.26
+        */
+       protected function getBindingHandle() {
+               if ( !$this->mConn ) {
+                       throw new DBUnexpectedError(
+                               $this,
+                               'DB connection was already closed or the connection dropped.'
+                       );
+               }
+
+               return $this->mConn;
+       }
+
        /**
         * @param string $oldName
         * @param string $newName