Ticket #5932: check-connection.4.diff
File check-connection.4.diff, 4.4 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 ); 1078 1079 return true; 1080 } 1081 1082 return false; 1083 } 1060 1084 1061 $this->set_charset( $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 $tries = 1; 1094 while ( ! mysql_ping( $this->dbh ) ) { 1095 if ( $this->db_connect() ) 1096 return true; 1062 1097 1063 $this->ready = true; 1098 $tries++; 1099 1100 if ( $tries >= $this->reconnect_retries ) { 1101 $this->bail( sprintf( __( " 1102 <h1>Error reconnecting to the database</h1> 1103 <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> 1104 <ul> 1105 <li>Are you sure that the database server is running?</li> 1106 <li>Are you sure that the database server is not under particularly heavy load?</li> 1107 </ul> 1108 <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> 1109 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); 1064 1110 1065 $this->select( $this->dbname, $this->dbh ); 1111 return false; 1112 } 1113 1114 sleep( 1 ); 1115 } 1116 1117 return true; 1066 1118 } 1067 1119 1120 1068 1121 /** 1069 1122 * Perform a MySQL database query, using current database connection. 1070 1123 * … … 1091 1144 // Keep track of the last query for debug.. 1092 1145 $this->last_query = $query; 1093 1146 1094 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1095 $this->timer_start(); 1147 $this->_do_query( $query ); 1148 1149 // MySQL server has gone away, try to reconnect 1150 if( mysql_errno( $this->dbh ) == 2006 ) { 1151 if( ! $this->check_connection() ) 1152 return false; 1096 1153 1097 $this->result = @mysql_query( $query, $this->dbh);1098 $this->num_queries++;1154 $this->_do_query( $query ); 1155 } 1099 1156 1100 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )1101 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );1102 1103 1157 // If there is an error then take note of it.. 1104 1158 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1105 1159 $this->print_error(); … … 1140 1194 } 1141 1195 1142 1196 /** 1197 * Internal function to perform the mysql_query call 1198 * 1199 * @since 3.5.0 1200 * 1201 * @access private 1202 * @see wpdb::query() 1203 * 1204 * @param string $query The query to run 1205 */ 1206 private function _do_query( $query ) { 1207 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1208 $this->timer_start(); 1209 1210 $this->result = @mysql_query( $query, $this->dbh ); 1211 $this->num_queries++; 1212 1213 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1214 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1215 } 1216 1217 /** 1143 1218 * Insert a row into a table. 1144 1219 * 1145 1220 * <code>