Make WordPress Core

Ticket #5932: check-connection.4.diff

File check-connection.4.diff, 4.4 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 );
     1078                       
     1079                        return true;
     1080                }
     1081               
     1082                return false;
     1083        }
    10601084
    1061                 $this->set_charset( $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                $tries = 1;
     1094                while ( ! mysql_ping( $this->dbh ) ) {
     1095                        if ( $this->db_connect() )
     1096                                return true;
    10621097
    1063                 $this->ready = true;
     1098                        $tries++;
     1099                       
     1100                        if ( $tries >= $this->reconnect_retries ) {
     1101                                $this->bail( sprintf( __( "
     1102<h1>Error reconnecting to the database</h1>
     1103<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>
     1104<ul>
     1105        <li>Are you sure that the database server is running?</li>
     1106        <li>Are you sure that the database server is not under particularly heavy load?</li>
     1107</ul>
     1108<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>
     1109" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
    10641110
    1065                 $this->select( $this->dbname, $this->dbh );
     1111                                return false;
     1112                        }
     1113                       
     1114                        sleep( 1 );
     1115                }
     1116               
     1117                return true;
    10661118        }
    10671119
     1120
    10681121        /**
    10691122         * Perform a MySQL database query, using current database connection.
    10701123         *
     
    10911144                // Keep track of the last query for debug..
    10921145                $this->last_query = $query;
    10931146
    1094                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1095                         $this->timer_start();
     1147                $this->_do_query( $query );
     1148               
     1149                // MySQL server has gone away, try to reconnect
     1150                if( mysql_errno( $this->dbh ) == 2006 ) {
     1151                        if( ! $this->check_connection() )
     1152                                return false;
    10961153
    1097                 $this->result = @mysql_query( $query, $this->dbh );
    1098                 $this->num_queries++;
     1154                        $this->_do_query( $query );
     1155                }
    10991156
    1100                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
    1101                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
    1102 
    11031157                // If there is an error then take note of it..
    11041158                if ( $this->last_error = mysql_error( $this->dbh ) ) {
    11051159                        $this->print_error();
     
    11401194        }
    11411195
    11421196        /**
     1197         * Internal function to perform the mysql_query call
     1198         *
     1199         * @since 3.5.0
     1200         *
     1201         * @access private
     1202         * @see wpdb::query()
     1203         *
     1204         * @param string $query The query to run
     1205         */
     1206        private function _do_query( $query ) {
     1207                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1208                        $this->timer_start();
     1209
     1210                $this->result = @mysql_query( $query, $this->dbh );
     1211                $this->num_queries++;
     1212
     1213                if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
     1214                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
     1215        }
     1216
     1217        /**
    11431218         * Insert a row into a table.
    11441219         *
    11451220         * <code>