Chapter 16. Singleton Pattern

My boss came to me one day and told me customers were complaining about the website being slow. I decided to do a few tests on the responsiveness of the website under a heavy load. I started out with 25 concurrent users. This was enough to crash the MySQL server. Through trial and error — and a very upset networking team — I was able to find that magic number where the website started showing strain. Six simultaneous users were all it could handle. Obviously, my boss told me to find out why. I was eager to solve this problem, too. The page I tested was rendering content from a Content Management System, which was nothing too complicated. After some investigation, I found out that two specific database systems were queried to build the front page. However, each page opened up more than ten connections to the same MySQL server and two to the same DB2 server. I examined the queries that were used to build these pages and discovered that their state did not need to persist throughout the whole page. The connection was opened, the information retrieved, and the link was then abandoned. Later on, a new connection was forced open for a new query. The only good news was that the code base was using an object-oriented approach to connect to the databases. After some refactoring, the objects used only one shared database connection to each type of server. Instead of generating a new connection whenever the database was called, I forced the objects to reuse ...

Get Professional PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.