I’m having a problem with MySQL going to sleep because a PHP script takes too long.. then the mysql server is unresponsive until its done with that sleep process. It then takes a minute to do any type of database request (faster if you remove the SLEEP process).
I use pconnect() to connect to the server, and I was told this might be causing the problem. Is that right, and if so, why would a persistent connection just stop working like this?
Persistent connections can leave ‘garbage’ behind if the previous user of the connection aborted abnormally. Consider the case where you’re doing a transaction with a series of individual queries. Half-way through, let’s assume the remote user killed the connection, which causes the script to abort. Because the mysql connection is persistent, it will not be closed when the script exits, and leaves the transaction open.
MySQL has no way of knowing it should do a roll-back, so it sits there with the transaction in play, waiting for another query to come in.
When this connection gets re-used by another script, that script also has no way of knowing that a transaction is in play, and will simply start issuing its own queries.
This can quite easily cause deadlocks, because of all these ‘stale’ transactions being left partially copmleted. If your scripts change any server-side settings for some reason, those settings will also be persisted on a per-connection basis, etc…
In other words, unless you’re EXTREMELY careful with your code, persistent connections will cause a long series of strange behaviors that are very difficult to diagnose, because each connection will be in a different state for every script that uses them.