Merge "API: Use message-per-value for apihelp-query+allusers-param-prop"
[lhc/web/wiklou.git] / includes / db / DatabaseMysqlBase.php
index a189648..be34242 100644 (file)
@@ -59,22 +59,16 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        function open( $server, $user, $password, $dbName ) {
                global $wgAllDBsAreLocalhost, $wgSQLMode;
 
-               # Debugging hack -- fake cluster
-               if ( $wgAllDBsAreLocalhost ) {
-                       $realServer = 'localhost';
-               } else {
-                       $realServer = $server;
-               }
+               # Close/unset connection handle
                $this->close();
+
+               # Debugging hack -- fake cluster
+               $realServer = $wgAllDBsAreLocalhost ? 'localhost' : $server;
                $this->mServer = $server;
                $this->mUser = $user;
                $this->mPassword = $password;
                $this->mDBname = $dbName;
 
-               # The kernel's default SYN retransmission period is far too slow for us,
-               # so we use a short timeout plus a manual retry. Retrying means that a small
-               # but finite rate of SYN packet loss won't cause user-visible errors.
-               $this->mConn = false;
                $this->installErrorHandler();
                try {
                        $this->mConn = $this->mysqlConnect( $realServer );
@@ -586,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;
@@ -943,7 +940,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                                $value = $this->mDefaultBigSelects;
                        }
                } elseif ( $this->mDefaultBigSelects === null ) {
-                       $this->mDefaultBigSelects = (bool)$this->selectField( false, '@@sql_big_selects' );
+                       $this->mDefaultBigSelects = (bool)$this->selectField( false, '@@sql_big_selects', '', __METHOD__ );
                }
                $encValue = $value ? '1' : '0';
                $this->query( "SET sql_big_selects=$encValue", __METHOD__ );
@@ -1058,6 +1055,32 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                        ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
        }
 
+       function wasConnectionError( $errno ) {
+               return $errno == 2013 || $errno == 2006;
+       }
+
+       /**
+        * 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