Ticket #5932: check-connection.6.diff
File check-connection.6.diff, 4.3 KB (added by , 12 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.5.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 … … 1126 1136 * Connect to and select database 1127 1137 * 1128 1138 * @since 3.0.0 1139 * 1140 * @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.5.0 1141 * 1142 * @return bool true on successful connection, false on unsuccessful connection 1129 1143 */ 1130 function db_connect( ) {1144 function db_connect( $allow_bail = true ) { 1131 1145 1132 1146 $this->is_mysql = true; 1133 1147 … … 1140 1154 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); 1141 1155 } 1142 1156 1143 if ( !$this->dbh ) {1157 if ( !$this->dbh && $allow_bail ) { 1144 1158 wp_load_translations_early(); 1145 1159 $this->bail( sprintf( __( " 1146 1160 <h1>Error establishing a database connection</h1> … … 1153 1167 <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> 1154 1168 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); 1155 1169 1156 return ;1170 return false; 1157 1171 } 1172 else if ( $this->dbh ) { 1173 $this->set_charset( $this->dbh ); 1174 $this->ready = true; 1175 $this->select( $this->dbname, $this->dbh ); 1158 1176 1159 $this->set_charset( $this->dbh ); 1177 return true; 1178 } 1160 1179 1161 $this->ready = true; 1180 return false; 1181 } 1162 1182 1163 $this->select( $this->dbname, $this->dbh ); 1183 /** 1184 * Check that the connection to the database is still up. If not, try to reconnect. 1185 * 1186 * @since 3.5.0 1187 * 1188 * @return bool true if the connection is up, false if it's down and we were unable to reconnect 1189 */ 1190 function check_connection() { 1191 if ( mysql_ping( $this->dbh ) ) 1192 return true; 1193 1194 for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) { 1195 if ( $this->db_connect( false ) ) 1196 return true; 1197 1198 sleep( 1 ); 1199 } 1200 1201 // We weren't able to reconnect, so we better bail 1202 $this->bail( sprintf( __( " 1203 <h1>Error reconnecting to the database</h1> 1204 <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> 1205 <ul> 1206 <li>Are you sure that the database server is running?</li> 1207 <li>Are you sure that the database server is not under particularly heavy load?</li> 1208 </ul> 1209 <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> 1210 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); 1211 1212 return false; 1164 1213 } 1165 1214 1166 1215 /** … … 1189 1238 // Keep track of the last query for debug.. 1190 1239 $this->last_query = $query; 1191 1240 1192 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1193 $this->timer_start(); 1241 $this->_do_query( $query ); 1194 1242 1195 $this->result = @mysql_query( $query, $this->dbh ); 1196 $this->num_queries++; 1243 // MySQL server has gone away, try to reconnect 1244 if( mysql_errno( $this->dbh ) == 2006 ) { 1245 if( ! $this->check_connection() ) 1246 return false; 1197 1247 1198 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )1199 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );1248 $this->_do_query( $query ); 1249 } 1200 1250 1201 1251 // If there is an error then take note of it.. 1202 1252 if ( $this->last_error = mysql_error( $this->dbh ) ) { … … 1231 1281 } 1232 1282 1233 1283 /** 1284 * Internal function to perform the mysql_query call 1285 * 1286 * @since 3.5.0 1287 * 1288 * @access private 1289 * @see wpdb::query() 1290 * 1291 * @param string $query The query to run 1292 */ 1293 private function _do_query( $query ) { 1294 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1295 $this->timer_start(); 1296 1297 $this->result = @mysql_query( $query, $this->dbh ); 1298 $this->num_queries++; 1299 1300 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1301 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1302 } 1303 1304 /** 1234 1305 * Insert a row into a table. 1235 1306 * 1236 1307 * <code>