Make WordPress Core

Ticket #5932: 5932.2.diff

File 5932.2.diff, 4.4 KB (added by wonderboymusic, 11 years ago)
  • src/wp-includes/wp-db.php

     
    158158        var $queries;
    159159
    160160        /**
     161         * The number of times to retry reconnecting before dying.
     162         *
     163         * @since 3.9.0
     164         * @access protected
     165         * @see wpdb::check_connection()
     166         * @var int
     167         */
     168        protected $reconnect_retries = 5;
     169
     170        /**
    161171         * WordPress table prefix
    162172         *
    163173         * You can set this to have multiple WordPress installations
     
    11301140         * Connect to and select database
    11311141         *
    11321142         * @since 3.0.0
     1143         *
     1144         * @param bool $allow_bail Optional. Allows the function to bail, default true. If this is set to false, you will need to handle the lack of database connection manually. Available since 3.7.0
     1145         *
     1146         * @return bool true on successful connection, false on unsuccessful connection
    11331147         */
    1134         function db_connect() {
     1148        function db_connect( $allow_bail = true ) {
    11351149
    11361150                $this->is_mysql = true;
    11371151
     
    11441158                        $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    11451159                }
    11461160
    1147                 if ( !$this->dbh ) {
     1161                if ( ! $this->dbh && $allow_bail ) {
    11481162                        wp_load_translations_early();
    11491163                        $this->bail( sprintf( __( "
    11501164<h1>Error establishing a database connection</h1>
     
    11571171<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
    11581172" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
    11591173
    1160                         return;
     1174                        return false;
     1175                } else if ( $this->dbh ) {
     1176                        $this->set_charset( $this->dbh );
     1177                        $this->ready = true;
     1178                        $this->select( $this->dbname, $this->dbh );
     1179
     1180                        return true;
    11611181                }
    11621182
    1163                 $this->set_charset( $this->dbh );
     1183                return false;
     1184        }
    11641185
    1165                 $this->ready = true;
     1186        /**
     1187         * Check that the connection to the database is still up. If not, try to reconnect.
     1188         *
     1189         * @since 3.9.0
     1190         *
     1191         * @return bool true if the connection is up, false if it's down and we were unable to reconnect
     1192         */
     1193        function check_connection() {
     1194                if ( mysql_ping( $this->dbh ) )
     1195                        return true;
    11661196
    1167                 $this->select( $this->dbname, $this->dbh );
     1197                for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {
     1198                        if ( $this->db_connect( false ) )
     1199                                return true;
     1200
     1201                        sleep( 1 );
     1202                }
     1203
     1204                // We weren't able to reconnect, so we better bail
     1205                $this->bail( sprintf( __( "
     1206<h1>Error reconnecting to the database</h1>
     1207<p>This means that we lost our connection to the database server at <code>%s</code>, your host's database server may be down.</p>
     1208<ul>
     1209        <li>Are you sure that the database server is running?</li>
     1210        <li>Are you sure that the database server is not under particularly heavy load?</li>
     1211</ul>
     1212<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
     1213" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
     1214
     1215                return false;
    11681216        }
    11691217
    11701218        /**
     
    11991247                // Keep track of the last query for debug..
    12001248                $this->last_query = $query;
    12011249
    1202                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1203                         $this->timer_start();
     1250                $this->_do_query( $query );
    12041251
    1205                 $this->result = @mysql_query( $query, $this->dbh );
    1206                 $this->num_queries++;
     1252                // MySQL server has gone away, try to reconnect
     1253                if ( 2006 == mysql_errno( $this->dbh ) ) {
     1254                        if( ! $this->check_connection() )
     1255                                return false;
    12071256
    1208                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1209                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1257                        $this->_do_query( $query );
     1258                }
    12101259
    12111260                // If there is an error then take note of it..
    12121261                if ( $this->last_error = mysql_error( $this->dbh ) ) {
     
    12451294        }
    12461295
    12471296        /**
     1297         * Internal function to perform the mysql_query call
     1298         *
     1299         * @since 3.9.0
     1300         *
     1301         * @access private
     1302         * @see wpdb::query()
     1303         *
     1304         * @param string $query The query to run
     1305         */
     1306        private function _do_query( $query ) {
     1307                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1308                        $this->timer_start();
     1309
     1310                $this->result = @mysql_query( $query, $this->dbh );
     1311                $this->num_queries++;
     1312
     1313                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1314                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1315        }
     1316
     1317        /**
    12481318         * Insert a row into a table.
    12491319         *
    12501320         * <code>