Make WordPress Core

Ticket #5932: 5932.4.diff

File 5932.4.diff, 5.2 KB (added by pento, 10 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.9.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
     
    11521166                        $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    11531167                }
    11541168
    1155                 if ( !$this->dbh ) {
     1169                if ( ! $this->dbh && $allow_bail ) {
    11561170                        wp_load_translations_early();
    11571171
    11581172                        // Load custom DB error template, if present.
     
    11721186<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>
    11731187" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
    11741188
    1175                         return;
     1189                        return false;
     1190                } else if ( $this->dbh ) {
     1191                        $this->set_charset( $this->dbh );
     1192                        $this->ready = true;
     1193                        $this->select( $this->dbname, $this->dbh );
     1194
     1195                        return true;
    11761196                }
    11771197
    1178                 $this->set_charset( $this->dbh );
     1198                return false;
     1199        }
    11791200
    1180                 $this->ready = true;
     1201        /**
     1202         * Check that the connection to the database is still up. If not, try to reconnect.
     1203         *
     1204         * @since 3.9.0
     1205         *
     1206         * @return bool true if the connection is up, false if it's down and we were unable to reconnect
     1207         */
     1208        function check_connection() {
     1209                if ( @mysql_ping( $this->dbh ) ) {
     1210                        return true;
     1211                }
    11811212
    1182                 $this->select( $this->dbname, $this->dbh );
     1213                for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {
     1214                        if ( $this->db_connect( false ) ) {
     1215                                return true;
     1216                        }
     1217
     1218                        sleep( 1 );
     1219                }
     1220
     1221                // We weren't able to reconnect, so we better bail
     1222                $this->bail( sprintf( __( "
     1223<h1>Error reconnecting to the database</h1>
     1224<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>
     1225<ul>
     1226        <li>Are you sure that the database server is running?</li>
     1227        <li>Are you sure that the database server is not under particularly heavy load?</li>
     1228</ul>
     1229<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>
     1230" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
     1231
     1232                return false;
    11831233        }
    11841234
    11851235        /**
     
    12141264                // Keep track of the last query for debug..
    12151265                $this->last_query = $query;
    12161266
    1217                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1218                         $this->timer_start();
     1267                $this->_do_query( $query );
    12191268
    1220                 $this->result = @mysql_query( $query, $this->dbh );
    1221                 $this->num_queries++;
     1269                // MySQL server has gone away, try to reconnect
     1270                if ( empty( $this->dbh ) || 2006 == @mysql_errno( $this->dbh ) ) {
     1271                        if( ! $this->check_connection() ) {
     1272                                return false;
     1273                        }
    12221274
    1223                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1224                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1275                        $this->_do_query( $query );
     1276                }
    12251277
    12261278                // If there is an error then take note of it..
    12271279                if ( $this->last_error = mysql_error( $this->dbh ) ) {
     
    12601312        }
    12611313
    12621314        /**
     1315         * Internal function to perform the mysql_query call
     1316         *
     1317         * @since 3.9.0
     1318         *
     1319         * @access private
     1320         * @see wpdb::query()
     1321         *
     1322         * @param string $query The query to run
     1323         */
     1324        private function _do_query( $query ) {
     1325                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
     1326                        $this->timer_start();
     1327                }
     1328
     1329                $this->result = @mysql_query( $query, $this->dbh );
     1330                $this->num_queries++;
     1331
     1332                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
     1333                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1334                }
     1335        }
     1336
     1337        /**
    12631338         * Insert a row into a table.
    12641339         *
    12651340         * <code>
  • tests/phpunit/tests/db.php

     
    4141        }
    4242
    4343        /**
     44         * Test that WPDB will reconnect when the DB link dies
     45         * @ticket 5932
     46         */
     47        public function test_db_reconnect() {
     48                global $wpdb;
     49
     50                $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1;" );
     51                $this->assertGreaterThan( 0, $var );
     52
     53                mysql_close( $wpdb->dbh );
     54                unset( $wpdb->dbh );
     55
     56                $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1;" );
     57                $this->assertGreaterThan( 0, $var );
     58        }
     59
     60        /**
    4461         * Test that floats formatted as "0,700" get sanitized properly by wpdb
    4562         * @global mixed $wpdb
    4663         *