Index: src/wp-includes/wp-db.php
===================================================================
--- src/wp-includes/wp-db.php	(revision 34307)
+++ src/wp-includes/wp-db.php	(working copy)
@@ -1541,11 +1541,11 @@
 	 */
 	public function check_connection( $allow_bail = true ) {
 		if ( $this->use_mysqli ) {
-			if ( @mysqli_ping( $this->dbh ) ) {
+			if ( !empty( $this->dbh ) && @mysqli_ping( $this->dbh ) ) {
 				return true;
 			}
 		} else {
-			if ( @mysql_ping( $this->dbh ) ) {
+			if ( !empty( $this->dbh ) && @mysql_ping( $this->dbh ) ) {
 				return true;
 			}
 		}
@@ -1651,6 +1651,12 @@
 		// Keep track of the last query for debug..
 		$this->last_query = $query;
 
+		// Try to reconnect if the db handle is empty to avoid catchable fatal error.
+		if ( empty( $this->dbh ) && !$this->check_connection() ) {
+			$this->insert_id = 0;
+			return false;
+		}
+
 		$this->_do_query( $query );
 
 		// MySQL server has gone away, try to reconnect
