Make WordPress Core

Ticket #5932: check-connection.6.diff

File check-connection.6.diff, 4.3 KB (added by pento, 12 years ago)
  • wp-includes/wp-db.php

     
    156156         * @var array
    157157         */
    158158        var $queries;
     159       
     160        /**
     161         * The number of times to retry reconnecting before dying.
     162         *
     163         * @since 3.5.0
     164         * @access protected
     165         * @see wpdb::check_connection()
     166         * @var int
     167         */
     168        protected $reconnect_retries = 5;
    159169
    160170        /**
    161171         * WordPress table prefix
     
    11261136         * Connect to and select database
    11271137         *
    11281138         * @since 3.0.0
     1139         *
     1140         * @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.5.0
     1141         *
     1142         * @return bool true on successful connection, false on unsuccessful connection
    11291143         */
    1130         function db_connect() {
     1144        function db_connect( $allow_bail = true ) {
    11311145
    11321146                $this->is_mysql = true;
    11331147
     
    11401154                        $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
    11411155                }
    11421156
    1143                 if ( !$this->dbh ) {
     1157                if ( !$this->dbh && $allow_bail ) {
    11441158                        wp_load_translations_early();
    11451159                        $this->bail( sprintf( __( "
    11461160<h1>Error establishing a database connection</h1>
     
    11531167<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>
    11541168" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
    11551169
    1156                         return;
     1170                        return false;
    11571171                }
     1172                else if ( $this->dbh ) {
     1173                        $this->set_charset( $this->dbh );
     1174                        $this->ready = true;
     1175                        $this->select( $this->dbname, $this->dbh );
    11581176
    1159                 $this->set_charset( $this->dbh );
     1177                        return true;
     1178                }
    11601179
    1161                 $this->ready = true;
     1180                return false;
     1181        }
    11621182
    1163                 $this->select( $this->dbname, $this->dbh );
     1183        /**
     1184         * Check that the connection to the database is still up. If not, try to reconnect.
     1185         *
     1186         * @since 3.5.0
     1187         *
     1188         * @return bool true if the connection is up, false if it's down and we were unable to reconnect
     1189         */
     1190        function check_connection() {
     1191                if ( mysql_ping( $this->dbh ) )
     1192                        return true;
     1193
     1194                for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {
     1195                        if ( $this->db_connect( false ) )
     1196                                return true;
     1197
     1198                        sleep( 1 );
     1199                }
     1200
     1201                // We weren't able to reconnect, so we better bail
     1202                $this->bail( sprintf( __( "
     1203<h1>Error reconnecting to the database</h1>
     1204<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>
     1205<ul>
     1206        <li>Are you sure that the database server is running?</li>
     1207        <li>Are you sure that the database server is not under particularly heavy load?</li>
     1208</ul>
     1209<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>
     1210" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
     1211
     1212                return false;
    11641213        }
    11651214
    11661215        /**
     
    11891238                // Keep track of the last query for debug..
    11901239                $this->last_query = $query;
    11911240
    1192                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1193                         $this->timer_start();
     1241                $this->_do_query( $query );
    11941242
    1195                 $this->result = @mysql_query( $query, $this->dbh );
    1196                 $this->num_queries++;
     1243                // MySQL server has gone away, try to reconnect
     1244                if( mysql_errno( $this->dbh ) == 2006 ) {
     1245                        if( ! $this->check_connection() )
     1246                                return false;
    11971247
    1198                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1199                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1248                        $this->_do_query( $query );
     1249                }
    12001250
    12011251                // If there is an error then take note of it..
    12021252                if ( $this->last_error = mysql_error( $this->dbh ) ) {
     
    12311281        }
    12321282
    12331283        /**
     1284         * Internal function to perform the mysql_query call
     1285         *
     1286         * @since 3.5.0
     1287         *
     1288         * @access private
     1289         * @see wpdb::query()
     1290         *
     1291         * @param string $query The query to run
     1292         */
     1293        private function _do_query( $query ) {
     1294                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1295                        $this->timer_start();
     1296
     1297                $this->result = @mysql_query( $query, $this->dbh );
     1298                $this->num_queries++;
     1299
     1300                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1301                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1302        }
     1303
     1304        /**
    12341305         * Insert a row into a table.
    12351306         *
    12361307         * <code>