Ticket #5932: check-connection.5.diff
File check-connection.5.diff, 4.3 KB (added by , 12 years ago) |
---|
-
wp-includes/wp-db.php
147 147 * @var array 148 148 */ 149 149 var $queries; 150 151 /** 152 * The number of times to retry reconnecting before dying. 153 * 154 * @since 3.5.0 155 * @access protected 156 * @see wpdb::check_connection() 157 * @var int 158 */ 159 protected $reconnect_retries = 5; 150 160 151 161 /** 152 162 * WordPress table prefix … … 1031 1041 * Connect to and select database 1032 1042 * 1033 1043 * @since 3.0.0 1044 * 1045 * @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 1046 * 1047 * @return bool true on successful connection, false on unsuccessful connection 1034 1048 */ 1035 function db_connect( ) {1049 function db_connect( $allow_bail = true ) { 1036 1050 1037 1051 $this->is_mysql = true; 1038 1052 … … 1042 1056 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 1043 1057 } 1044 1058 1045 if ( !$this->dbh ) {1059 if ( !$this->dbh && $allow_bail ) { 1046 1060 wp_load_translations_early(); 1047 1061 $this->bail( sprintf( __( " 1048 1062 <h1>Error establishing a database connection</h1> … … 1055 1069 <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> 1056 1070 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); 1057 1071 1058 return ;1072 return false; 1059 1073 } 1074 else if ( $this->dbh ) { 1075 $this->set_charset( $this->dbh ); 1076 $this->ready = true; 1077 $this->select( $this->dbname, $this->dbh ); 1060 1078 1061 $this->set_charset( $this->dbh ); 1079 return true; 1080 } 1062 1081 1063 $this->ready = true; 1082 return false; 1083 } 1064 1084 1065 $this->select( $this->dbname, $this->dbh ); 1085 /** 1086 * Check that the connection to the database is still up. If not, try to reconnect. 1087 * 1088 * @since 3.5.0 1089 * 1090 * @return bool true if the connection is up, false if it's down and we were unable to reconnect 1091 */ 1092 function check_connection() { 1093 if ( mysql_ping( $this->dbh ) ) 1094 return true; 1095 1096 for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) { 1097 if ( $this->db_connect() ) 1098 return true; 1099 1100 sleep( 1 ); 1101 } 1102 1103 // We weren't able to reconnect, so we better bail 1104 $this->bail( sprintf( __( " 1105 <h1>Error reconnecting to the database</h1> 1106 <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> 1107 <ul> 1108 <li>Are you sure that the database server is running?</li> 1109 <li>Are you sure that the database server is not under particularly heavy load?</li> 1110 </ul> 1111 <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> 1112 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); 1113 1114 return false; 1066 1115 } 1067 1116 1068 1117 /** … … 1091 1140 // Keep track of the last query for debug.. 1092 1141 $this->last_query = $query; 1093 1142 1094 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1095 $this->timer_start(); 1143 $this->_do_query( $query ); 1096 1144 1097 $this->result = @mysql_query( $query, $this->dbh ); 1098 $this->num_queries++; 1145 // MySQL server has gone away, try to reconnect 1146 if( mysql_errno( $this->dbh ) == 2006 ) { 1147 if( ! $this->check_connection() ) 1148 return false; 1099 1149 1100 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )1101 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );1150 $this->_do_query( $query ); 1151 } 1102 1152 1103 1153 // If there is an error then take note of it.. 1104 1154 if ( $this->last_error = mysql_error( $this->dbh ) ) { … … 1140 1190 } 1141 1191 1142 1192 /** 1193 * Internal function to perform the mysql_query call 1194 * 1195 * @since 3.5.0 1196 * 1197 * @access private 1198 * @see wpdb::query() 1199 * 1200 * @param string $query The query to run 1201 */ 1202 private function _do_query( $query ) { 1203 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1204 $this->timer_start(); 1205 1206 $this->result = @mysql_query( $query, $this->dbh ); 1207 $this->num_queries++; 1208 1209 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1210 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1211 } 1212 1213 /** 1143 1214 * Insert a row into a table. 1144 1215 * 1145 1216 * <code>