I wrote about closing PDO connections recently. Specifically, I showed how to set $pdo = null at the end of your script to free resources. The comments section lit up with questions about persistent connections.

People asked why I wasn’t using them. Why waste time establishing a new connection for every request? It sounds logical. You connect once, keep it open, and reuse it. Faster, right?

I tried that on a project a few years ago. I thought it was free performance. I turned it on, watched the CPU usage drop slightly, and called it a day.

Then the bugs started. Subtle, weird, hard-to-reproduce bugs that only happened under load. It took me months to track them down to the one setting I thought made my app faster.

It wasn’t fast. It was just quiet. And that silence is where the problems hide.


How you actually turn it on

This isn’t a magic switch you flip after connecting. You can’t just call setAttribute later and expect it to work. The connection has to be created with the intent of being persistent from the very first byte.

You pass it in the constructor options:

$pdo = new PDO($dsn, $user, $pass, [
    PDO::ATTR_PERSISTENT => true,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

That’s it. If you try to set PDO::ATTR_PERSISTENT after the connection is established, PHP ignores you. The connection is already born. You can’t change its nature now.

This trips people up because it feels like a property you can toggle. It’s not. It’s a birth certificate.


What it really does (and what it doesn’t)

When a request ends, a normal PDO connection closes. The TCP socket hangs up. The database sees the client go away. Clean break.

A persistent connection doesn’t do that. When the script ends, the connection stays open. It lives inside the PHP-FPM worker process. The next request that gets assigned to that same worker grabs the existing connection and uses it.

Here is the part that breaks my brain every time I explain this: setting $pdo = null does not close a persistent connection.

You can set it to null, destroy the variable, even unset($pdo). The underlying TCP socket is still open. It’s owned by the worker process now, not your script. Your script just stops holding a reference to it.

This is counterintuitive. We treat object destruction as resource cleanup in PHP. With persistent connections, that mental model breaks. The resource outlives the code that created it.


Leftover state is your enemy

This is where I found my bug.

Imagine Request A runs a raw LOCK TABLES statement. It does some UPDATE statements. Then it hits a fatal error. An exception isn’t caught, or the process crashes hard. The script dies.

In a normal connection world, that’s fine. The connection closes. MySQL releases the table locks. The slate is clean.

With a persistent connection? The connection stays open. The lock is still held.

Request B comes in. It gets assigned to the same worker. It grabs the “free” connection. It thinks it has a clean state. It doesn’t. It inherits Request A’s active lock.

I had a weird issue where a specific table was locked for no reason. I couldn’t find the lock in the process list because the “process” holding it was a previous request that had already returned 500 to the user. The lock was ghost data, living in a persistent connection that shouldn’t have existed.

This applies to more than just locks. If you run SET SESSION sql_mode = ... or create temporary tables, those live in the session state. If you don’t clean them up perfectly, they leak into the next request.

One bad request can poison the next ten if you’re not careful.


You are multiplying your connection count

There is a math problem here that people ignore.

You have 4 web servers. Each runs PHP-FPM. Let’s say pm.max_children = 50. That means each server has up to 50 worker processes alive at any time.

If every worker holds a persistent connection, you have 50 open connections per server. Multiply that by 4 servers. You now have 200 idle database connections sitting there, doing nothing, just waiting for the next request.

MySQL has max_connections. The default is often 151. Some people crank it up to 500 or 1000 because they think they need more headroom.

If your app scales to 10 servers with 50 workers each, you’re looking at 500 persistent connections just from the web tier. Add in your admin panel, your cron jobs, your other microservices. You hit max_connections and suddenly new requests fail with “Too many connections.”

You didn’t scale your database. You scaled your connection leak.


The “server has gone away” problem

MySQL has a setting called wait_timeout. By default, it’s 8 hours. That sounds generous. But in production, we often lower it to keep resources tight. Maybe 300 seconds. Maybe 60.

If your persistent connection sits idle for longer than wait_timeout, MySQL closes it from its side. It sends a FIN packet. It forgets the connection exists.

PHP doesn’t always know this immediately. The next time your script uses that connection, it sends a query. The TCP stack says “connection reset” or “broken pipe.”

You get the classic error: MySQL server has gone away.

This is frustrating because it looks like a random network failure. It’s not. It’s a mismatch between when PHP thinks the connection is alive and when MySQL actually closed it.

With normal connections, this is a non-issue. You connect fresh every time. No stale state. No “has it timed out?” checks needed in your code logic.


When it actually helps

I’m not saying persistent connections are garbage. They have a specific use case.

If your database is far away. Like, really far. Different data centers. Different continents. And you’re using TLS.

The TCP handshake and the TLS handshake take time. If that handshake takes 50ms, and your script runs for 100ms, half your request time is just saying “hello” to the database.

In that scenario, reusing the connection saves you that 50ms every single time. That’s real money.

But if your database is on the same network, or even the same machine? The handshake is 1-2ms. You’re saving milliseconds by avoiding a millisecond-cost operation. And you’re paying for it in state management and connection counting.

Measure it. Don’t guess.

$start = microtime(true);
$pdo = new PDO($dsn, $user, $pass);
$end = microtime(true);

printf("Connection time: %.4f ms\n", ($end - $start) * 1000);

If that number is under 5ms, persistent connections are probably costing you more than they save. If it’s 50ms or higher, they might be worth the headache.


What I do instead

I use normal connections. Every request creates a new PDO object. I create it once at the start of my application lifecycle and pass it around via dependency injection. I don’t connect inside loops. I don’t reconnect if I don’t have to.

If connection overhead actually hurts performance (and I mean, we have graphs to prove it), I put a pooler in front of the database.

For MySQL, that’s ProxySQL. For Postgres, it’s PgBouncer.

These tools sit between your app and the database. They manage a pool of connections. Your app talks to the pooler. The pooler talks to the database. The pooler handles the handshakes, the timeouts, the state cleanup.

It does what persistent connections try to do, but it does it correctly. It isolates the state per connection properly. It manages the count centrally. You don’t have to worry about PHP-FPM worker limits dictating your DB load.

It’s a better solution to the same problem.


Leave them off

Persistent connections aren’t a speed setting. They’re a shared state mechanism between requests.

They save you the cost of a handshake. But they charge you in complexity, potential state leaks, and connection count bloat.

I know it feels like “free performance.” I used to think that too. But free things usually have a price tag somewhere else.

Turn them off. Use normal connections. If you’re slow, measure the connection time. If it’s genuinely high, add a pooler.

That’s it. Stop letting PHP pretend to be a database pool manager. It’s not good at it.