Make WordPress Core

Ticket #5932: check-connection.5.diff

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

     
    147147         * @var array
    148148         */
    149149        var $queries;
     150       
     151        /**
     152         * The number of times to retry reconnecting before dying.
     153         *
     154         * @since 3.5.0
     155         * @access protected
     156         * @see wpdb::check_connection()
     157         * @var int
     158         */
     159        protected $reconnect_retries = 5;
    150160
    151161        /**
    152162         * WordPress table prefix
     
    10311041         * Connect to and select database
    10321042         *
    10331043         * @since 3.0.0
     1044         *
     1045         * @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
     1046         *
     1047         * @return bool true on successful connection, false on unsuccessful connection
    10341048         */
    1035         function db_connect() {
     1049        function db_connect( $allow_bail = true ) {
    10361050
    10371051                $this->is_mysql = true;
    10381052
     
    10421056                        $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
    10431057                }
    10441058
    1045                 if ( !$this->dbh ) {
     1059                if ( !$this->dbh && $allow_bail ) {
    10461060                        wp_load_translations_early();
    10471061                        $this->bail( sprintf( __( "
    10481062<h1>Error establishing a database connection</h1>
     
    10551069<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>
    10561070" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
    10571071
    1058                         return;
     1072                        return false;
    10591073                }
     1074                else if ( $this->dbh ) {
     1075                        $this->set_charset( $this->dbh );
     1076                        $this->ready = true;
     1077                        $this->select( $this->dbname, $this->dbh );
    10601078
    1061                 $this->set_charset( $this->dbh );
     1079                        return true;
     1080                }
    10621081
    1063                 $this->ready = true;
     1082                return false;
     1083        }
    10641084
    1065                 $this->select( $this->dbname, $this->dbh );
     1085        /**
     1086         * Check that the connection to the database is still up. If not, try to reconnect.
     1087         *
     1088         * @since 3.5.0
     1089         *
     1090         * @return bool true if the connection is up, false if it's down and we were unable to reconnect
     1091         */
     1092        function check_connection() {
     1093                if ( mysql_ping( $this->dbh ) )
     1094                        return true;
     1095
     1096                for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {
     1097                        if ( $this->db_connect() )
     1098                                return true;
     1099
     1100                        sleep( 1 );
     1101                }
     1102
     1103                // We weren't able to reconnect, so we better bail
     1104                $this->bail( sprintf( __( "
     1105<h1>Error reconnecting to the database</h1>
     1106<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>
     1107<ul>
     1108        <li>Are you sure that the database server is running?</li>
     1109        <li>Are you sure that the database server is not under particularly heavy load?</li>
     1110</ul>
     1111<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>
     1112" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
     1113
     1114                return false;
    10661115        }
    10671116
    10681117        /**
     
    10911140                // Keep track of the last query for debug..
    10921141                $this->last_query = $query;
    10931142
    1094                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1095                         $this->timer_start();
     1143                $this->_do_query( $query );
    10961144
    1097                 $this->result = @mysql_query( $query, $this->dbh );
    1098                 $this->num_queries++;
     1145                // MySQL server has gone away, try to reconnect
     1146                if( mysql_errno( $this->dbh ) == 2006 ) {
     1147                        if( ! $this->check_connection() )
     1148                                return false;
    10991149
    1100                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1101                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1150                        $this->_do_query( $query );
     1151                }
    11021152
    11031153                // If there is an error then take note of it..
    11041154                if ( $this->last_error = mysql_error( $this->dbh ) ) {
     
    11401190        }
    11411191
    11421192        /**
     1193         * Internal function to perform the mysql_query call
     1194         *
     1195         * @since 3.5.0
     1196         *
     1197         * @access private
     1198         * @see wpdb::query()
     1199         *
     1200         * @param string $query The query to run
     1201         */
     1202        private function _do_query( $query ) {
     1203                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1204                        $this->timer_start();
     1205
     1206                $this->result = @mysql_query( $query, $this->dbh );
     1207                $this->num_queries++;
     1208
     1209                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1210                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1211        }
     1212
     1213        /**
    11431214         * Insert a row into a table.
    11441215         *
    11451216         * <code>