Persistent connection to Mysql database

Asked

Viewed 931 times

4

Hello would like to know if I keep an open database connection(persistent connection) is better than I always close after a query?

1 answer

6

That depends on a lot. Multiple instances (Keep connection open) can be useful if there is a large continuous demand, or if the database access control and supported operations (Insert, alter, delete) is done by database users, or even if the database requests are distributed. On the other hand, a single instance, in addition to being easier to manage, serves a reasonable number of users well with sporadic requests (which is the case in many systems).

But you close and open the connection with the bank can have a noticeable cost in performance, because the resources spent (time and processing) to load the driver and access the bank several times can cause unwanted slowness, even more so if many consecutive requests are made.

You could do a Singleton to solve your case. Follow this guide here which is very good:

http://www.devmedia.com.br/php-singleton-aplicando-o-padrao-de-projeto-na-pratica/28469

Browser other questions tagged

You are not signed in. Login or sign up in order to post.