LAMP with PHP5: When a Singleton is Not Quite a Singleton · 5 September 2007, 21:13
After a few days of puzzling over why my little singleton PHP5 data access layer was continuing to produce leftover database connections and produce more than just one connection (which is what I would expect a Singleton pattern to create!), I learned the following, mostly from this PHP manual page and user comments (one of the best features of the online PHP manual pages!):
http://us.php.net/manual/en/features.persistent-connections.php
- Even with a singleton class and mysql_pconnect, you will have multiple connections open on the server if you use a multi-process web server like Apache (as opposed to a single instance, multi-threaded server like IIS or Apache in it’s multi-threaded worker mode) as MySQL connection links are not shared between web server instances.
It would seem this could somehow be resolved by using shared memory for those connections, but I know that has it’s own problems and adds complexity.
- In your my.cnf file, if you do not set wait_timeout to some reasonably low number of seconds, those leftover persistent connections can hang out for a LOOOOONG time.
— Max Schubert
