Make WordPress Core

Ticket #6305: wp-db.diff

File wp-db.diff, 2.6 KB (added by westi, 18 years ago)

Update patch to not ping if the reconnect fails

  • wp-includes/wp-db.php

     
    4343                        'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
    4444        var $charset;
    4545        var $collate;
     46        var $db_user;
     47        var $db_password;
     48        var $db_name;
     49        var $db_host;
    4650
    4751        /**
    4852         * Connects to the database server and selects a database
     
    6771                if ( defined('DB_COLLATE') )
    6872                        $this->collate = DB_COLLATE;
    6973
    70                 $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
    71                 if (!$this->dbh) {
     74                $this->db_user = $dbuser;
     75                $this->db_password = $dbpassword;
     76                $this->db_name = $dbname;
     77                $this->db_host = $dbhost;
     78
     79                $this->connect_to_db();
     80        }
     81
     82        function __destruct() {
     83                return true;
     84        }
     85
     86        function connect_to_db() {
     87                $this->dbh = @mysql_connect($this->db_host, $this->db_user, $this->db_password, true);
     88                if ( !$this->dbh ) {
    7289                        $this->bail("
    7390<h1>Error establishing a database connection</h1>
    7491<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
     
    87104                if ( !empty($this->charset) && version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=') )
    88105                        $this->query("SET NAMES '$this->charset'");
    89106
    90                 $this->select($dbname);
     107                $this->select($this->db_name);
    91108        }
    92109
    93         function __destruct() {
    94                 return true;
     110        function check_connection() {
     111                $maxcount = 2;
     112                $count = 0;
     113
     114                $ping = mysql_ping( $this->dbh ) ;
     115
     116                while ( !$ping && $count < $maxcount ) {
     117                        @mysql_close($this->dbh);
     118                        sleep(2);
     119                        if ($this->connect_to_db() )
     120                        {
     121                                $ping = mysql_ping( $this->dbh ) ;
     122
     123                                if ( $ping )
     124                                        break;
     125                        }
     126                        sleep(2);
     127                        $count++;
     128                }
     129
     130                if ( !$ping )
     131                        $this->bail('Lost connection to server');
    95132        }
    96 
     133 
    97134        function set_prefix($prefix) {
    98135
    99136                if ( preg_match('|[^a-z0-9_]|i', $prefix) )
     
    280317
    281318                // If there is an error then take note of it..
    282319                if ( $this->last_error = mysql_error($this->dbh) ) {
    283                         $this->print_error();
    284                         return false;
     320                        $errno = mysql_errno();
     321                        if ( 2006 == $errno || 2013 == $errno ) {
     322                                $this->check_connection();
     323                                $this->result = @mysql_query($query, $this->dbh);
     324                                if ( $this->last_error = mysql_error($this->dbh) ) {
     325                                        $this->print_error();
     326                                        return false;
     327                                }
     328                        } else {
     329                                $this->print_error();
     330                                return false;
     331                        }
    285332                }
    286333
    287334                if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {