SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post DAO and Singleton

    I've been looking at the DAO pattern on phppatterns and from previously looking at Singletons I would think that the DAO pattern should in itself include the Singleton pattern, but the phppatterns' example code is in PHP4. I was wondering where (if anywhere) the Singleton pattern would fit in? How does the DAO pattern handle too many db connections (if that is a problem as I am lead to believe)?

  2. #2
    SitePoint Enthusiast rikdc's Avatar
    Join Date
    Sep 2005
    Location
    Edinburgh
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not entirely sure what your asking, but regarding handling too many connections - rather than having the dao establish a connection you could try passing a reference to the database connection in your dao's contstructor.

    Trying to think of reasons why you'd want to use a singleton in a dao - I've used them in a mapper where there's been an identity map to cache records already loaded from the database in that particular page it.

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How does the DAO pattern handle too many db connections
    Answered as above. The database connection should be your concern, and it's this that should be implemented as a Singleton; not the actual DAO it's self...

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, ok. That makes sense. Thanks.

  5. #5
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mijokijo
    I've been looking at the DAO pattern on phppatterns and from previously looking at Singletons I would think that the DAO pattern should in itself include the Singleton pattern, but the phppatterns' example code is in PHP4. I was wondering where (if anywhere) the Singleton pattern would fit in? How does the DAO pattern handle too many db connections (if that is a problem as I am lead to believe)?
    The problem of too database many connections is not something that you can solve in a per request basis.

    What happens is that your Web server is creating too many processes that are establishishing persistent connections to the database. Each process only establishes one connection to the same database server, but these connections are not closed until the process is finished. So, if your server can only accept a limited number of database connections, what you need to do is to limit the number of opened Web server processes.

    There are several things you can do like serving all static content (images and CSS) with a separate Web server as these files do not need database connections to be served but if you have many images in a page it causes the Web server to create an high number of processes that will keep persistent database connections when they serve PHP pages that require database access.

    Another thing you should do is to limit the number of simultaneous HTTP requests that your server will accept. In Apache that is done setting the MaxClients option. During a surge, exceeding connection requests are enqueud. This causes the effect similar to having a connection pool. So, there is no need to use database access middleware like SQLrelay that provides connection pooling. Adequate PHP and Apache configuration will work the same way and faster, as the usage of middleware will only add overhead that can be avoided the way I described.

    You also need to make exceeding processes created during surges die after a surge. There are a few Apache settings to configure and make this happen automatically.

    <advisor edit>Removed product advertising</advisor edit>
    Last edited by Helge; Feb 16, 2006 at 08:39.
    Manuel Lemos

    Metastorage - Data object relational mapping layer generator
    PHP Classes - Free ready to use OOP components in PHP


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •