Question

I build an application (MySQL/PHP) which (in time) may use connections to distant (and maybe slow) MySQL servers. The app uses AJAX whispers which open the DB connections frequently. I have done some client side optimization, but never had courage enough to use persistent connections, which may be good idea here.

I am worried about idle mysql connections which may exceed the server limit resulting in need to restart the server. So I plan to use mysql.connection_timeout to 15s, which will kill every idle persistent connection after the limit? Or will it block system resources until the end of times of server process?

I won't use transactions in whispers and every lock should be released after the connection is closed, right?

Are there any other persistent connection issues I should be aware of? (I have read the documentation already.)

Was it helpful?

Solution

You don't generally gain anything by using persistent connections with MySQL. Its connection protocol is already generally fairly fast. What you do lose is the guarantee of a "clean" operating environment when you use regular non-persistent connections.

If any of your scripts die for any reason while in the middle of a transaction or leave server-side variables set, a persistent connection means that half-done transaction is still active when the connection is picked up again by some OTHER script.

You can quite easily fall into a deadlock situation when this happens. The new script has no idea that it's got an old/dirty connection, and MySQL cannot see that the original connecting script has gone away and do its own cleanup, so you end up with a garbage strewn environment.

That being said, you can limit how many connections PHP will keep open on your behalf with the mysql.max_persistent setting. No matter how many connections you attemp to make from your scripts, PHP will never exceed that limit, so you can set it to be lower than MySQL's own max_connections setting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top