Support » Developing with WordPress » Should plugins close $wpdb via $wpdb->close()

  • Hello,

    I’ve been writing a few custom plugins for my site and I was wondering about the correct way to interact with $wpdb.

    My plugin executes a few queries using the global $wpdb connection.
    I noticed that a $wpdb->close(); method has been added to WordPress, to close the database connection object.

    My question is: Should a plugin close out the connection after executing queries?
    Or is it sufficient to simply close out the prepared statement, and flush the connection, but allow $wpdb to maintain its connection.

    i.e., Should I execute this before every call to $wpdb
    $wpdb->check_connection();
    To re-establish the connection if required.
    And then follow up after I’m done with calls to close the connection:

    function my_close_statement($statement) {
      try {
        if (!empty($statement)) {
          $statement->free_result();
          $statement->close();
        }
      } catch (Exception $e) {
      }
    }
    function my_close_database($dbconn, $statement=null) {
      my_close_statement($statement);
      try {
        if (!empty($dbconn)) {
          $dbconn->flush();
          $dbconn->close();
        }    
      } catch (Exception $e) {
      }
    }

    Additional Info: My reason for asking is that I am worried that I am not cleaning statements/results/connections up properly, and that this may be causing issues with Yeost, and JetPack. I’m seeing a lot of these in my debug log:

    “WordPress database error Commands out of sync; you can’t run this command now for query SELECT”

    Which appear to be Yeost and Jetpack complaints.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Is your plugin doing something that would affect the connection?
    There is a plugin for database maintenance, like optimizing the tables. Doing this from the admin, that’s the only thing going on for that page request, so the plugin could (and maybe should) treat the connection differently than normal use. I’m thinking that normal use is reading. More rare is writing.
    But most page requests for the front end are reading and would involve multiple plugins. WP caches a lot of the data, so it shouldn’t matter if you close the connection, but one plugin out of many should not micromanage the database. $wpdb is a global variable, and it manages itself.
    Back end page requests are more confined to one plugin or another, except for the major sort of writing to the database which is the editor, and many plugins could be involved. Again, none should be micromanaging a global resource unless that’s what the plugin is for.

    Thanks for the reply! I started to get the sense that this was the case, and that my code didn’t work anyway… $wpdb->prepare returns a string, not a prepared statement (I’m used to the JDBC Java world).

    OK My plugin is purely obtaining some cart data, and updating a custom table. So it sounds like I should not bother closing any connections, as I am not doing anything funky with the database really. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.