How do I verify that WordPress database connections are closed using PHP?

I would like to add a test using PHP’s register_shutdown_function to verify that all database connections are closed. Additionally, I would like to automatically close these connections. I am on a shared hosting environment and thus do not have much system access.

The current site is experiencing horrible performance issues (broken access or >60 sec page loads) particularly when under a load exceeding 10 visitors. The slow downs affect admin screens and occur even when all plugins are disabled. Yet there are times when performance is less than 8 sec page load. Our hosting provider has responded by saying that we have open database connections that sometimes require manual closing.

Read More

I know that WordPress defers closing database connections to PHP and it should not create these kind of issues yet what else could it be.

Related posts

Leave a Reply

3 comments

  1. Disclaimer: I’m not a WordPress user. My knowledge about WordPress comes from articles I googled now, some of them are really old.

    wpdb

    WordPress provides wpdb class and its global instance $wpdb. If I get it right, this instance is used both internally and by plugins to perform all database manipulation.

    Explicitly closing wpdb connection

    wpdb has no close method. From How to close wpdb connection? at WordPress Answers:

    There is no explicit method. It stores link identifier in $wpdb->dbh field, so I guess you could use mysql_close() on it.

    It looks like WordPress is still using the outdated mysql API nowadays. There are three MySQL APIs in PHP now. If/when WordPress migrates to a more recent one, inform me in comments and I’ll update this answer and the answer in the original question.

    Automatic closing, persistent connections

    Are you using persistent connections? They cannot be closed, they persist after the script finishes its execution.

    When using non-persistent connections, they should be closed on script end. Quoting mysql_connect function manual page:

    WordPress relies on that, connections are not closed explicitly. If you want to explicitly close the connection when destroying $wpdb, change

    function __destruct() {
        return true;
    }
    

    to

    function __destruct() {
        mysql_close($this->dbh);
        return true;
    }
    

    in wp-includes/wp-db.php.

    Verifying that connections are closed

    If you have a list of connections to be checked/closed, you can verify that they already have been closed by calling mysql_close with error suppression and return value checking:

    if (@mysql_close($conn)) {
        echo "Closed nown";
    } else {
        echo "Probably already closedn";
    }
    

    When not having such a list, you are doomed. There is no way to list all DB connections made by the current script. You can only list all connections to a server that you have an active connection to, but not as PHP resources. If you want to close them, you can do that using KILL statement if you have high enough privileges. Remember, though, that the connections may have been created and may be being used by other scripts.

    See also

  2. PHP use managed resources, which means resources (including DB connections) are automatically released when the script exits. Unless you use persistent connections but WordPress doesn’t use persistent connections.

    I also had this kind of issues on a VPS with the hosting provider telling me I had to close connections, the solution was to add page caching.