WordPress.org

Make WordPress Core

Changeset 36433


Ignore:
Timestamp:
02/02/2016 12:12:28 AM (5 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r36385 r36433  
    30943094    }
    30953095
     3096
     3097    /**
     3098     * Closes the current database connection.
     3099     *
     3100     * @since 4.5.0
     3101     *
     3102     * @return bool True if the connection was succesfully closed, false if it wasn't, or the connection doesn't exist.
     3103     */
     3104    public function close() {
     3105        if ( ! $this->dbh ) {
     3106            return false;
     3107        }
     3108
     3109        if ( $this->use_mysqli ) {
     3110            $closed = mysqli_close( $this->dbh );
     3111        } else {
     3112            $closed = mysql_close( $this->dbh );
     3113        }
     3114
     3115        if ( $closed ) {
     3116            $this->dbh = null;
     3117        }
     3118
     3119        return $closed;
     3120    }
     3121
    30963122    /**
    30973123     * Whether MySQL database is at least the required minimum version.
  • trunk/tests/phpunit/tests/db.php

    r35242 r36433  
    942942        $this->assertNull( $row );
    943943    }
     944
     945    /**
     946     * @ticket 34903
     947     */
     948    function test_close() {
     949        global $wpdb;
     950
     951        $this->assertTrue( $wpdb->close() );
     952        $this->assertFalse( $wpdb->close() );
     953
     954        $wpdb->check_connection();
     955
     956        $this->assertTrue( $wpdb->close() );
     957
     958        $wpdb->check_connection();
     959    }
    944960}
Note: See TracChangeset for help on using the changeset viewer.