To close or not close MySQL???

A

Anonymous

Guest
Is it a good idea to close MySQL databases as soon as data manipulation is completed for posting a page or not? Would too many opening and closing of databases slow the respond time? What might be the advantages and disadvantages of having a database open in a multi user and transactional database?
Thanks :?
 
I think they get closed one way or another when the script completes. for larger scripts, then a more active approach should be taken against memory leaks.
 
You're right sadisticmachine!

Non-persistent database connections (opposite to persistent ones) last only while a database-related request is processed, thus reducing the load on the database server.

It's not that important to close them!
 
I would add that the problem with non-persistent connections is that connecting to a db is usually a very resource- consuming operation, so that with other databases persistent connections are prefered. However, one advantage of mysql is the low impact of connecting to a db, thus making the use of non-persistent connections more convenient.
By the way, if I remember well, persistent connections cannot be closed and expire by timeout with a parameter set in php.ini.

Cheers.
 
im not really sure about them not being closed as there is a function that closes any connection... so i believe they CAN be closed if needed
 
even persistant connections aren't that big a deal, since PHP won't open more than one persistant connection on the same user, so if you're going to be calling the page many times, its good to have the connection around.
 
If there is no advantage of having the DB open then I assume closing the database is a good practice especially if there is possibility of losing connection or even server shutdown or failure. You do not want your database open when the sever crashes or there is any possibility of power failure.
 
the connectio is terminated when the scrip excecution is done.. therfore in most cases its simply to use mysql_connect()

no need to close the connection..
 
victor123 said:
persistent connections cannot be closed and expire by timeout with a parameter set in php.ini.
No, there's no need to be closed. That's why there's no mysql_pclose() and the mysql_close() doesn't work with it.
However, persistent conections can be closed, setting a low wait_timeout in MySQL.

The diference here is that, once the non-persistent connection are close at the end, no re-use of the connection link identifier will be done. The same don't happens with the persistent ones. That's why they're called persistent.
 
You are right, gesf, there is not mysql_pclose() and the only way of closing them is by changing the timeout in mysql. With mysql i would not use persistent connections because if they are not released and managed properly via a pool of connections, you have the risk of consuming all the available connections (you can fix the max number of connections in mysql, however you cannot do that in shared hosting).
The price of simplicity is consuming an extra overhead caused by mysql_connect at the top of each page and by mysql_close at the bottom (even if it is not called explicitly). However, this overhead is negligible since mysql is optimized for that.

Regards
 
Back
Top