From 5779ed9d8a5c6b58362bbb3ee7c7579427acb55f Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Tue, 2 Feb 2016 00:13:26 +0000 Subject: [PATCH] WPDB: Add a `close()` method to `wpdb`, for when the connection needs to be manually closed. In the event that it was closed prematurely, `wpdb::query()` will re-open the connection automatically. Fixes #34903. Built from https://develop.svn.wordpress.org/trunk@36433 git-svn-id: http://core.svn.wordpress.org/trunk@36400 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/version.php | 2 +- wp-includes/wp-db.php | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/wp-includes/version.php b/wp-includes/version.php index 5b0781a980..9aed5e450a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-36432'; +$wp_version = '4.5-alpha-36433'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 1356a76163..f0ed7d9d34 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -3093,6 +3093,32 @@ class wpdb { wp_die($message); } + + /** + * Closes the current database connection. + * + * @since 4.5.0 + * + * @return bool True if the connection was succesfully closed, false if it wasn't, or the connection doesn't exist. + */ + public function close() { + if ( ! $this->dbh ) { + return false; + } + + if ( $this->use_mysqli ) { + $closed = mysqli_close( $this->dbh ); + } else { + $closed = mysql_close( $this->dbh ); + } + + if ( $closed ) { + $this->dbh = null; + } + + return $closed; + } + /** * Whether MySQL database is at least the required minimum version. *