I see many tutorial about connection db is open a connection, and do the stuff, and close the connection in the end. I would like to know whether this is true for real life example?
I know it can get the job done when I open, and close the connection every time. But will it have some performance issue? Moreover, can I reuse the opened connection? Or is this inevitable to keep open and closing the db?? Thanks.
*I am using php + mysql, thanks.
you can use
mysql_pconnectif you want a persistent connection:http://php.net/manual/en/function.mysql-pconnect.php
Though, in my experience, I don’t like this. If you’re running your database and webserver on the same host, then the time to set up a connection is very VERY small. I initialize a connection for every visitor with minimal cost.
If the mysql database is remote relative to your website, you might want to consider this. Setting up a connection takes multiple round trips. So if you ping WEBSERVER to MYSQLSERVER and it’s 100ms. It’s going to take multiple times that to get a connection. In this case, it might be worth using a persistent connection.