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