Make WordPress Core

Ticket #5932: 5932.diff

File 5932.diff, 4.3 KB (added by pento, 11 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.7.0
     164         * @access protected
     165         * @see wpdb::check_connection()
     166         * @var int
     167         */
     168        protected $reconnect_retries = 5;
    159169
    160170        /**
    161171         * WordPress table prefix
     
    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                }
     1176                else if ( $this->dbh ) {
     1177                        $this->set_charset( $this->dbh );
     1178                        $this->ready = true;
     1179                        $this->select( $this->dbname, $this->dbh );
     1180
     1181                        return true;
    11611182                }
    11621183
    1163                 $this->set_charset( $this->dbh );
     1184                return false;
     1185        }
    11641186
    1165                 $this->ready = true;
     1187        /**
     1188         * Check that the connection to the database is still up. If not, try to reconnect.
     1189         *
     1190         * @since 3.7.0
     1191         *
     1192         * @return bool true if the connection is up, false if it's down and we were unable to reconnect
     1193         */
     1194        function check_connection() {
     1195                if ( mysql_ping( $this->dbh ) )
     1196                        return true;
     1197
     1198                for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {
     1199                        if ( $this->db_connect( false ) )
     1200                                return true;
     1201
     1202                        sleep( 1 );
     1203                }
     1204
     1205                // We weren't able to reconnect, so we better bail
     1206                $this->bail( sprintf( __( "
     1207<h1>Error reconnecting to the database</h1>
     1208<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>
     1209<ul>
     1210        <li>Are you sure that the database server is running?</li>
     1211        <li>Are you sure that the database server is not under particularly heavy load?</li>
     1212</ul>
     1213<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>
     1214" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
    11661215
    1167                 $this->select( $this->dbname, $this->dbh );
     1216                return false;
    11681217        }
    11691218
    11701219        /**
     
    11991248                // Keep track of the last query for debug..
    12001249                $this->last_query = $query;
    12011250
    1202                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1203                         $this->timer_start();
     1251                $this->_do_query( $query );
    12041252
    1205                 $this->result = @mysql_query( $query, $this->dbh );
    1206                 $this->num_queries++;
     1253                // MySQL server has gone away, try to reconnect
     1254                if( mysql_errno( $this->dbh ) == 2006 ) {
     1255                        if( ! $this->check_connection() )
     1256                                return false;
    12071257
    1208                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1209                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1258                        $this->_do_query( $query );
     1259                }
    12101260
    12111261                // If there is an error then take note of it..
    12121262                if ( $this->last_error = mysql_error( $this->dbh ) ) {
     
    12451295        }
    12461296
    12471297        /**
     1298         * Internal function to perform the mysql_query call
     1299         *
     1300         * @since 3.7.0
     1301         *
     1302         * @access private
     1303         * @see wpdb::query()
     1304         *
     1305         * @param string $query The query to run
     1306         */
     1307        private function _do_query( $query ) {
     1308                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1309                        $this->timer_start();
     1310
     1311                $this->result = @mysql_query( $query, $this->dbh );
     1312                $this->num_queries++;
     1313
     1314                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1315                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1316        }
     1317
     1318        /**
    12481319         * Insert a row into a table.
    12491320         *
    12501321         * <code>