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.



git-svn-id: https://develop.svn.wordpress.org/trunk@36433 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2016-02-02 00:12:28 +00:00
parent 15002741b5
commit 2918cca22c
2 changed files with 42 additions and 0 deletions

View File

@ -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.
*

View File

@ -941,4 +941,20 @@ class Tests_DB extends WP_UnitTestCase {
$this->assertNull( $row );
}
/**
* @ticket 34903
*/
function test_close() {
global $wpdb;
$this->assertTrue( $wpdb->close() );
$this->assertFalse( $wpdb->close() );
$wpdb->check_connection();
$this->assertTrue( $wpdb->close() );
$wpdb->check_connection();
}
}