Author Topic: Database class  (Read 471 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Database class
« on: August 24, 2008, 09:21:03 AM »
This question is probably easier to answer if you know a bit about the singleton design pattern.

Should this pattern be applied to a database class?

I've never needed more than one database connection, but would there be any problems with applying this design to a database class?

In what kind of situation would you need more than one database connection?

Offline Dasein Fiasco

  • Level 15
  • *
  • Posts: 132
  • Reputation: +2/-0
    • View Profile
    • Travis Dunn
Re: Database class
« Reply #1 on: August 24, 2008, 11:41:51 AM »
I don't know about the latest version of PHP if that's what you're using, but most frameworks now handle connection pooling behind the scenes so I'd first check to ensure that it's not already being done for you.

Now, personally, I would not apply a singleton pattern to your DB connection manager or whatever it is you're creating. First, if something screws up for one user, it screws up for everyone, unless you have some graceful way of dealing with the singleton instance's exceptions. Maybe this is irrelevant in your situation, depending on complexity, but philosophically I'd want to keep things separate and just instantiate a new class as needed.

I would also ask: do you actually NEED a singleton DB object? What does the application of this pattern to DB connections give you? The singleton, like most GOF design patterns, is really the providence of an application, not a DB. If fact, there's a well known phenomenon called impedance mismatch (http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch which occurs when you try to apply OOP design principles to a relational DB. This is more relevant if by "database class" you mean a class that represents a table in the DB, but still applies in this case where the pattern doesn't appear to give you anything valuable, and potentially opens you up for some obscure errors should things go wrong.

As for multiple DB connections, on a number of cases I've worked with applications that aggregate data from a wide variety of sources, including several different databases instances AND several different database applications. I'm guessing this is beside the point for your situation, but it's certainly quite commonly done in the industry.
Personal Site: www.travisdunn.com
There is neither speech nor language but their voice is heard among them

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal