0

Normally, a good googling session would suffice to answer most of my questions, but $this isn't one of them. Thus, it's is my first question here :

A lot of people (at SO and elsewhere) say that Singletton is bad. Actually so bad that some fellow developers even consider it as an Anti-Christ Anti-Pattern and should be replaced by Dependency Injection Pattern. The only exception to this 'rule' is with loggers (well, almost).

Some argue that because in PHP variables are at most 1 request-old ...

[...] one of the two main purposes of a Singleton is not applicable here.

But during that one request, multiple fetches from the Database may/will occur. Database is a shared resource between ALL the requests and if I don't make sure that there's as few connections as possible, I may get struck with a max_connections error (just an example).

So, if Singleton is a bad design in most cases, what about managing Database connections ? Is Singleton a good idea or I should opt for DI ?

Thanks in advance : )

Community
  • 1
  • 1
  • 1
    DI doesn't preclude a single database connection: you DI the actual connection class with its existing connection – Mark Baker Sep 23 '13 at 22:16
  • Singleton is bad for testing as previous tests influence current tests.. You are enforced to test using process isolation if you take care on that. For your purpose Singleton isn't suited at all – hek2mgl Sep 23 '13 at 22:20
  • possible duplicate of [Who needs singletons?](http://stackoverflow.com/questions/4595964/who-needs-singletons) and/or [Use global variables in a class](http://stackoverflow.com/questions/11923272/use-global-variables-in-a-class/11923384#11923384) – PeeHaa Sep 23 '13 at 22:22
  • @PeeHaa : Thanks for the (second) link, it's weird how I didn't find it. – عمر بو الزور Sep 23 '13 at 22:30
  • And for the "This question may already have an answer here:" I really would choose the second linked provided by @PeeHaa. It's the one that suites better my question. – عمر بو الزور Sep 23 '13 at 22:31

2 Answers2

2

A Singleton is a bad design in this case. Google would say it's a bad idea in all cases.

The right answer here is the connection pool.

You can avoid a max connections issue by configuring your pool to remain well below the limit and by making sure that your code rigorously closes all connections as soon as you're done with them. The cycle should be very short: check out of pool, execute SQL, close connection.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Yes this is how it's done already, only no special pattern design was used. And since the app is getting bigger and more complex I thought that an upgrade to ensure future flexibility would be appreciated. – عمر بو الزور Sep 23 '13 at 22:36
  • What makes you think that using a pattern makes anything better? – duffymo Sep 24 '13 at 00:15
  • In fact, I considered using Singleton because it will help me avoid having to reconnect to the database each time I need to fetch some data (which happens a lot of time in my app and causes a lot of time wasting). How can a DI implementation help me do the same thing ? – عمر بو الزور Sep 24 '13 at 12:01
  • A pool creates several connections and stores them, amortizing the connection cost over all your calls. This is how pools work. Go with the pool and forget the singleton idea. It cannot scale. You sound like you've been infected with "small boy with a pattern" syndrome. – duffymo Sep 24 '13 at 12:08
  • Hehe, that's a beautiful name for a syndrome :p Though, the more I search about connections pooling in PHP the more I find sources stating that it's not possible (at least with MySQL, since some other vendors-specific DBs allow it). Is that true ? If not, would you please show where I can learn more about connections pooling in PHP and MySQL ? Thanks : ) – عمر بو الزور Sep 24 '13 at 22:11
0

The Singleton pattern for database connection is also bad idea. How many connection to database you use (or need) depends on your application design.

You can use same instance of database connection class in whole request, but without using singleton pattern.

kormik
  • 839
  • 8
  • 18