4

I'm building something with Silex and Doctrine DBAL using a Mysql database.

I was wondering if it considered best practice to close the cursor of a statement and database connection after one's done it. For example:

$sql = '
    SELECT
        `id`, `userid`, `name`, `content` 
    FROM 
        `pages` p 
    WHERE 
        p.`userid` in (?)'; 

$stmt = $conn->executeQuery(
    $sql,
    [$userIds],
    [\Doctrine\DBAL\Connection::PARAM_INT_ARRAY]
);

$rows = $stmt->fetchAll();

$stmt->closeCursor();

$conn->close();

I can't find any examples of this. Both the Silex and Doctrine doc's don't use the statements but they're there for a reason right?

Tieme
  • 62,602
  • 20
  • 102
  • 156

1 Answers1

2

$stmt->closeCursor() appears to just be a wrapper around PDOStatement::closeCursor() and would allow you to reexecute the query if you needed to:

PDOStatement::closeCursor() frees up the connection to the server so that other SQL statements may be issued, but leaves the statement in a state that enables it to be executed again.

This method is useful for database drivers that do not support executing a PDOStatement object when a previously executed PDOStatement object still has unfetched rows. If your database driver suffers from this limitation, the problem may manifest itself in an out-of-sequence error.

In the example you posted, you are not executing multiple queries and instead closing the connection immediately afterwords. Thus, calling it is not beneficial. If you're really concerned about memory usage, setting $stmt to null should be more effective.

Connection::close() appears to just sets the connection on the object to null:

/**
 * Closes the connection.
 *
 * @return void
 */
public function close()
{
    $this->_conn = null;
    $this->_isConnected = false;
}

It frees up some memory, thus making it a better practice than not calling it. However I would expect the benefit to be negligible as it will be closed when the script terminates anyway.

HPierce
  • 7,249
  • 7
  • 33
  • 49
  • Hmm, yeah but the `$conn->close();` call is also not listed in their docs.. My question was about closing the db as well... – Tieme Oct 03 '15 at 19:19
  • @Tieme, Maybe I'm not following - is `$conn` _not_ an instance of `Connection`? Is that not the "db" you are referring to? – HPierce Oct 03 '15 at 20:56
  • Yes it is an instance of `Connection`, and indeed the database (connection) I was referring to. – Tieme Oct 20 '15 at 13:48
  • 1
    If the doc says: `PDOStatement::closeCursor() frees up the connection to the server so that other SQL statements may be issued` then it should be called right? – Tieme Oct 20 '15 at 13:49
  • 1
    @Tieme, I can see why you'd think that based on what I copied over. I've updated the answer - but the purpose for `PDOStatement::closeCursor()` is more about supporting fringe DBs than optimization. IMO, the note about setting `$stmt = null;` is overkill, but should be worth trying over closing the cursor. – HPierce Oct 20 '15 at 14:24