Reputation: 773
I noticed there is no close function for PDO. Should I close the connection or is it unnecessary for PDO?
Upvotes: 34
Views: 41845
Reputation: 142298
What I get from this is [roughly]
The issues are
TEMPORARY
tablesUSE
)I would like to see a clear specification of what is or is not done with those issues in every type of connection pooling (PDO, MariaDB, various Proxies, etc.) Quotes from man pages are not specific enough for me to trust.
Upvotes: 0
Reputation: 72672
This question is depending a bit on the type of project and the type of connection.
In almost all of my projects I never manually close the connection. In PHP the connection (unless it is a persistent connection) will only be open during the request. So manually closing it is pretty useless anyway.
When looking at my projects where there was no persistent connection it would have been very hard to know when to manually close the connection either way. Once a project gets larger than a couple of files (and the individual components have no idea about eachother like they should) it becomes very hard to know when the connection will still be needed.
And opening the connection again when needed is waay more expensive than just leaving it open during the request.
Something though when working with persistent connection there will be situations where you will want to manually close the connection.
So to answer your question:
I noticed there is no close function for PDO.
You can nullify the object reference (and all references to the object) to manually close the connection in PHP.
Should I close the connection or is it unnecessary for PDO?
In most situations it is not necessary.
Upvotes: 13
Reputation: 4024
Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.
http://php.net/manual/en/pdo.connections.php
So the answer is no, you don't need to do anything unless you need to explicitly close the connection during the script execution for whatever reason, in which case just set your PDO object to null.
Upvotes: 46
Reputation: 80639
From the PDO's connection page
Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object.
If you don't do this explicitly, PHP will automatically close the connection when your script ends.
I'd rather use persistent connection. Though, it's a good practice to close all connections at the end of the script.
Upvotes: 5