Closing MySQL connection after each request

While digging some old code compiled by a former employee whose talents I was looking for, I noticed that after each request they closed the connection to the MySQL server.

This seems a little strange since I always closed it at the end of the page and saw how most people do the same.

So the question is, is this a better idea in PHP / MySQL? Does it even matter anyway? Only about 100 users work at this site at any time during the day. Therefore, I do not see any real impact that this could have anyway, but perhaps this would make sense for a larger site?

+4
source share
2 answers

This can be useful when working with an application that needs to be scaled for many concurrent users. By doing so, you open the connection only for a short time (i.e. only when using it).

If you had 500 simultaneous queries, they might be better negotiated so that one query could perform calculations on the PHP side and the other blocked access to the database.

+4
source

Closing and opening connections after each request is slower than just using one connection. Maybe closing the connection before the long process becomes a good idea, so you have little chance of reaching the limit of connections, but with simple pages your method, I think, is really better.

However, I do not know how many users you will begin to notice the difference.

+3
source

Source: https://habr.com/ru/post/1313763/


All Articles