• 5 Subscribers

Consider the following PHP snippet:

$dbw = new db_writer($table_name);       // establishes a PDO connection
$success = $dbw->insert_record($record);

Does unsetting the $dbw object result in the PDO connection closing?

The whole point of my db_writer class is to preserve the connection so that, if desired, the class can perform multiple operations without having to make/break the connection for each operation. Thus the class does not ever unset the PDO object, which I presume does close the connection.

1 answers in total

SergeyLebedev Posted at 2017-01-11 19:57:17Z

According to PDO documentation when you set PDO object value to null it closes the connection. Hovewer there can be situations when some object keeps reference to PDO object instance. In this case connection will be closed only at the end of PHP script execution.

Also there is PDO::ATTR_PERSISTENT options that allows you to create persistent PDO connection that will not be closed on script execution end and can be reused on the next script run.

Answer this questsion