Ticket #5932: check-connection.2.diff

File check-connection.2.diff, 3.3 KB (added by pento, 10 months ago)
Line 
1Index: wp-includes/wp-db.php
2===================================================================
3--- wp-includes/wp-db.php       (revision 21466)
4+++ wp-includes/wp-db.php       (working copy)
5@@ -147,6 +147,16 @@
6         * @var array
7         */
8        var $queries;
9+       
10+       /**
11+        * The number of times to retry reconnecting before dying.
12+        *
13+        * @since 3.5.0
14+        * @access protected
15+        * @see wpdb::check_connection()
16+        * @var int
17+        */
18+       protected $reconnect_retries = 5;
19 
20        /**
21         * WordPress table prefix
22@@ -1066,6 +1076,50 @@
23        }
24 
25        /**
26+        * Check that the connection to the database is still up. If not, try to reconnect.
27+        *
28+        * @since 3.5.0
29+        */
30+       function check_connection() {
31+               $tries = 1;
32+               while ( ! mysql_ping( $this->dbh ) ) {
33+                       if ( WP_DEBUG ) {
34+                               $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
35+                       } else {
36+                               $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
37+                       }
38+
39+                       if ( $this->dbh ) {
40+                               $this->set_charset( $this->dbh );
41+                               $this->select( $this->dbname, $this->dbh );
42+
43+                               return true;
44+                       }
45+                               
46+                       $tries++;
47+                       
48+                       if ( $tries >= $this->reconnect_retries ) {
49+                               $this->bail( sprintf( __( "
50+<h1>Error reconnecting to the database</h1>
51+<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>
52+<ul>
53+       <li>Are you sure that the database server is running?</li>
54+       <li>Are you sure that the database server is not under particularly heavy load?</li>
55+</ul>
56+<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>
57+" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
58+
59+                               return false;
60+                       }
61+                       
62+                       sleep( 1 );
63+               }
64+               
65+               return true;
66+       }
67+
68+
69+       /**
70         * Perform a MySQL database query, using current database connection.
71         *
72         * More information can be found on the codex page.
73@@ -1091,15 +1145,16 @@
74                // Keep track of the last query for debug..
75                $this->last_query = $query;
76 
77-               if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
78-                       $this->timer_start();
79+               $this->_do_query( $query );
80+               
81+               // MySQL server has gone away, try to reconnect
82+               if( mysql_errno == 2006 ) {
83+                       if( ! $this->check_connection() )
84+                               return false;
85 
86-               $this->result = @mysql_query( $query, $this->dbh );
87-               $this->num_queries++;
88+                       $this->_do_query( $query );
89+               }
90 
91-               if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
92-                       $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
93-
94                // If there is an error then take note of it..
95                if ( $this->last_error = mysql_error( $this->dbh ) ) {
96                        $this->print_error();
97@@ -1140,6 +1195,27 @@
98        }
99 
100        /**
101+        * Internal function to perform the mysql_query call
102+        *
103+        * @since 3.5.0
104+        *
105+        * @access private
106+        * @see wpdb::query()
107+        *
108+        * @param string $query The query to run
109+        */
110+       private function _do_query( $query ) {
111+               if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
112+                       $this->timer_start();
113+
114+               $this->result = @mysql_query( $query, $this->dbh );
115+               $this->num_queries++;
116+
117+               if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
118+                       $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
119+       }
120+
121+       /**
122         * Insert a row into a table.
123         *
124         * <code>