Index: wp-includes/wp-db.php
===================================================================
--- wp-includes/wp-db.php	(revision 21466)
+++ wp-includes/wp-db.php	(working copy)
@@ -147,6 +147,16 @@
 	 * @var array
 	 */
 	var $queries;
+	
+	/**
+	 * The number of times to retry reconnecting before dying.
+	 *
+	 * @since 3.5.0
+	 * @access protected
+	 * @see wpdb::check_connection()
+	 * @var int
+	 */
+	protected $reconnect_retries = 5;
 
 	/**
 	 * WordPress table prefix
@@ -1066,6 +1076,50 @@
 	}
 
 	/**
+	 * Check that the connection to the database is still up. If not, try to reconnect.
+	 *
+	 * @since 3.5.0
+	 */
+	function check_connection() {
+		$tries = 1;
+		while ( ! mysql_ping( $this->dbh ) ) {
+			if ( WP_DEBUG ) {
+				$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
+			} else {
+				$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
+			}
+
+			if ( $this->dbh ) {
+				$this->set_charset( $this->dbh );
+				$this->select( $this->dbname, $this->dbh );
+
+				return true;
+			}
+				
+			$tries++;
+			
+			if ( $tries >= $this->reconnect_retries ) {
+				$this->bail( sprintf( __( "
+<h1>Error reconnecting to the database</h1>
+<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>
+<ul>
+	<li>Are you sure that the database server is running?</li>
+	<li>Are you sure that the database server is not under particularly heavy load?</li>
+</ul>
+<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>
+" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
+
+				return false;
+			}
+			
+			sleep( 1 );
+		}
+		
+		return true;
+	}
+
+
+	/**
 	 * Perform a MySQL database query, using current database connection.
 	 *
 	 * More information can be found on the codex page.
@@ -1091,15 +1145,16 @@
 		// Keep track of the last query for debug..
 		$this->last_query = $query;
 
-		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
-			$this->timer_start();
+		$this->_do_query( $query );
+		
+		// MySQL server has gone away, try to reconnect
+		if( mysql_errno == 2006 ) {
+			if( ! $this->check_connection() )
+				return false;
 
-		$this->result = @mysql_query( $query, $this->dbh );
-		$this->num_queries++;
+			$this->_do_query( $query );
+		}
 
-		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
-			$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
-
 		// If there is an error then take note of it..
 		if ( $this->last_error = mysql_error( $this->dbh ) ) {
 			$this->print_error();
@@ -1140,6 +1195,27 @@
 	}
 
 	/**
+	 * Internal function to perform the mysql_query call
+	 *
+	 * @since 3.5.0
+	 *
+	 * @access private
+	 * @see wpdb::query()
+	 *
+	 * @param string $query The query to run
+	 */
+	private function _do_query( $query ) {
+		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
+			$this->timer_start();
+
+		$this->result = @mysql_query( $query, $this->dbh );
+		$this->num_queries++;
+
+		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
+			$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
+	}
+
+	/**
 	 * Insert a row into a table.
 	 *
 	 * <code>
