Ticket #5932: 5932.2.diff
File 5932.2.diff, 4.4 KB (added by , 11 years ago) |
---|
-
src/wp-includes/wp-db.php
158 158 var $queries; 159 159 160 160 /** 161 * The number of times to retry reconnecting before dying. 162 * 163 * @since 3.9.0 164 * @access protected 165 * @see wpdb::check_connection() 166 * @var int 167 */ 168 protected $reconnect_retries = 5; 169 170 /** 161 171 * WordPress table prefix 162 172 * 163 173 * You can set this to have multiple WordPress installations … … 1130 1140 * Connect to and select database 1131 1141 * 1132 1142 * @since 3.0.0 1143 * 1144 * @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.7.0 1145 * 1146 * @return bool true on successful connection, false on unsuccessful connection 1133 1147 */ 1134 function db_connect( ) {1148 function db_connect( $allow_bail = true ) { 1135 1149 1136 1150 $this->is_mysql = true; 1137 1151 … … 1144 1158 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1145 1159 } 1146 1160 1147 if ( ! $this->dbh) {1161 if ( ! $this->dbh && $allow_bail ) { 1148 1162 wp_load_translations_early(); 1149 1163 $this->bail( sprintf( __( " 1150 1164 <h1>Error establishing a database connection</h1> … … 1157 1171 <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> 1158 1172 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); 1159 1173 1160 return; 1174 return false; 1175 } else if ( $this->dbh ) { 1176 $this->set_charset( $this->dbh ); 1177 $this->ready = true; 1178 $this->select( $this->dbname, $this->dbh ); 1179 1180 return true; 1161 1181 } 1162 1182 1163 $this->set_charset( $this->dbh ); 1183 return false; 1184 } 1164 1185 1165 $this->ready = true; 1186 /** 1187 * Check that the connection to the database is still up. If not, try to reconnect. 1188 * 1189 * @since 3.9.0 1190 * 1191 * @return bool true if the connection is up, false if it's down and we were unable to reconnect 1192 */ 1193 function check_connection() { 1194 if ( mysql_ping( $this->dbh ) ) 1195 return true; 1166 1196 1167 $this->select( $this->dbname, $this->dbh ); 1197 for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) { 1198 if ( $this->db_connect( false ) ) 1199 return true; 1200 1201 sleep( 1 ); 1202 } 1203 1204 // We weren't able to reconnect, so we better bail 1205 $this->bail( sprintf( __( " 1206 <h1>Error reconnecting to the database</h1> 1207 <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> 1208 <ul> 1209 <li>Are you sure that the database server is running?</li> 1210 <li>Are you sure that the database server is not under particularly heavy load?</li> 1211 </ul> 1212 <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> 1213 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); 1214 1215 return false; 1168 1216 } 1169 1217 1170 1218 /** … … 1199 1247 // Keep track of the last query for debug.. 1200 1248 $this->last_query = $query; 1201 1249 1202 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1203 $this->timer_start(); 1250 $this->_do_query( $query ); 1204 1251 1205 $this->result = @mysql_query( $query, $this->dbh ); 1206 $this->num_queries++; 1252 // MySQL server has gone away, try to reconnect 1253 if ( 2006 == mysql_errno( $this->dbh ) ) { 1254 if( ! $this->check_connection() ) 1255 return false; 1207 1256 1208 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )1209 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );1257 $this->_do_query( $query ); 1258 } 1210 1259 1211 1260 // If there is an error then take note of it.. 1212 1261 if ( $this->last_error = mysql_error( $this->dbh ) ) { … … 1245 1294 } 1246 1295 1247 1296 /** 1297 * Internal function to perform the mysql_query call 1298 * 1299 * @since 3.9.0 1300 * 1301 * @access private 1302 * @see wpdb::query() 1303 * 1304 * @param string $query The query to run 1305 */ 1306 private function _do_query( $query ) { 1307 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1308 $this->timer_start(); 1309 1310 $this->result = @mysql_query( $query, $this->dbh ); 1311 $this->num_queries++; 1312 1313 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1314 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1315 } 1316 1317 /** 1248 1318 * Insert a row into a table. 1249 1319 * 1250 1320 * <code>