Make WordPress Core

Ticket #6305: wpdb_ping.diff

File wpdb_ping.diff, 2.6 KB (added by ryan, 18 years ago)
  • 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                        $this->connect_to_db();
     119
     120                        $ping = mysql_ping( $this->dbh ) ;
     121
     122                        if ( $ping )
     123                                break;
     124
     125                        sleep(2);
     126                        $count++;
     127                }
     128
     129                if ( !$ping )
     130                        $this->bail('Lost connection to server');
    95131        }
    96 
     132 
    97133        function set_prefix($prefix) {
    98134
    99135                if ( preg_match('|[^a-z0-9_]|i', $prefix) )
     
    280316
    281317                // If there is an error then take note of it..
    282318                if ( $this->last_error = mysql_error($this->dbh) ) {
    283                         $this->print_error();
    284                         return false;
     319                        $errno = mysql_errno();
     320                        if ( 2006 == $errno || 2013 == $errno ) {
     321                                $this->check_connection();
     322                                $this->result = @mysql_query($query, $this->dbh);
     323                                if ( $this->last_error = mysql_error($this->dbh) ) {
     324                                        $this->print_error();
     325                                        return false;
     326                                }
     327                        } else {
     328                                $this->print_error();
     329                                return false;
     330                        }
    285331                }
    286332
    287333                if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {