fixed connection error handling
authorTim Starling <tstarling@users.mediawiki.org>
Sun, 14 Aug 2005 04:42:55 +0000 (04:42 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Sun, 14 Aug 2005 04:42:55 +0000 (04:42 +0000)
includes/Database.php
includes/LoadBalancer.php

index 2b986a6..55932b5 100644 (file)
@@ -260,7 +260,6 @@ class Database {
 
                if ( !$success ) {
                        $this->reportConnectionError();
-                       $this->close();
                }
                $this->mOpened = $success;
                return $success;
@@ -289,16 +288,15 @@ class Database {
        /**
         * @access private
         * @param string $msg error message ?
-        * @todo parameter $msg is not used
         */
-       function reportConnectionError( $msg = '') {
+       function reportConnectionError() {
                if ( $this->mFailFunction ) {
                        if ( !is_int( $this->mFailFunction ) ) {
                                $ff = $this->mFailFunction;
                                $ff( $this, $this->lastError() );
                        }
                } else {
-                       wfEmergencyAbort( $this, mysql_error() );
+                       wfEmergencyAbort( $this, $this->lastError() );
                }
        }
 
@@ -614,7 +612,13 @@ class Database {
         */
        function lastError() {
                if ( $this->mConn ) {
+                       # Even if it's non-zero, it can still be invalid
+                       wfSuppressWarnings();
                        $error = mysql_error( $this->mConn );
+                       if ( !$error ) {
+                               $error = mysql_error();
+                       }
+                       wfRestoreWarnings();
                } else {
                        $error = mysql_error();
                }
@@ -1671,7 +1675,7 @@ class ResultWrapper {
  */
 function wfEmergencyAbort( &$conn, $error ) {
        global $wgTitle, $wgUseFileCache, $title, $wgInputEncoding, $wgOutputEncoding;
-       global $wgSitename, $wgServer;
+       global $wgSitename, $wgServer, $wgMessageCache;
 
        # I give up, Brion is right. Getting the message cache to work when there is no DB is tricky.
        # Hard coding strings instead.
@@ -1713,6 +1717,10 @@ border=\"0\" ALT=\"Google\"></A>
                header( 'Cache-control: none' );
                header( 'Pragma: nocache' );
        }
+
+       # No database access
+       $wgMessageCache->disable();
+       
        $msg = wfGetSiteNotice();
        if($msg == '') {
                $msg = str_replace( '$1', $error, $noconnect );
index 54fc617..c716d39 100644 (file)
@@ -27,7 +27,7 @@ define( 'DB_WRITE', -2 );
  */
 class LoadBalancer {
        /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
-       /* private */ var $mFailFunction;
+       /* private */ var $mFailFunction, $mErrorConnection;
        /* private */ var $mForce, $mReadIndex, $mLastIndex;
        /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
        /* private */ var $mLaggedSlaveMode;
@@ -40,6 +40,7 @@ class LoadBalancer {
                $this->mReadIndex = -1;
                $this->mForce = -1;
                $this->mLastIndex = -1;
+               $this->mErrorConnection = false;
        }
 
        function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
@@ -348,6 +349,10 @@ class LoadBalancer {
                                $i = $this->getWriterIndex();
                        }
                }
+               # Couldn't find a working server in getReaderIndex()?
+               if ( $i === false ) {
+                       $this->reportConnectionError( $this->mErrorConnection );
+               }
                # Now we have an explicit index into the servers array
                $this->openConnection( $i, $fail );
 
@@ -375,6 +380,7 @@ class LoadBalancer {
                        if ( $fail ) {
                                $this->reportConnectionError( $this->mConnections[$i] );
                        }
+                       $this->mErrorConnection = $this->mConnections[$i];
                        $this->mConnections[$i] = false;
                        $success = false;
                }
@@ -435,7 +441,7 @@ class LoadBalancer {
                        if ( $this->mFailFunction ) {
                                $conn->failFunction( $this->mFailFunction );
                        } else {
-                               $conn->failFunction( 'wfEmergencyAbort' );
+                               $conn->failFunction( false );
                        }
                        $conn->reportConnectionError();
                        $reporting = false;