PHP PDO Persistent connections to MySQL

TLDR: Can Persistent DB connections work properly on InfinityFree?

InfinityFree has MySQL max_user_connections set to 9.
I wanted to use PC, but i see in MySQL processlist that they create 1 connection per request. This makes the database unusable after 9 requests until they time out.

According to this SO answer it means that a PHP process is created for each request, which seems redundant and causes PC to not work as intended.

So can PHP behavior be configured in .htaccess to reuse the spawned processes?

TL;DR: Persistent database connections don’t work in PHP. On any hosting provider. And this is fine.

It’s true, every time a request comes it, it spawns a new PHP process, and every PHP process creates it’s own database connection.

However, do note that when the PHP script completes, all the database connections created from the script are closed as well. Unless the connection was closed from within the script already, of course.

And most PHP scripts don’t run for more than a few hundred milliseconds to handle a single request. So the number of requests per second your website can handle is actually quite reasonable.

So the only case where this would be a problem would be when more than 9 PHP requests come in at almost the exact same time. With a couple of dozen people just browsing your website at the same time, this is very unlikely to happen.

While some software can persist database connections and reuse them in other requests, this is basically impossible to do with PHP. To keep the database alive, there needs to be a continuously running PHP process to hold on to that process. While it’s theoretically possible to use PHP as it’s own process manager, these tools are all very experimental.

3 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.