SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    maintain mySQL connection

    Hi there,

    I was wondering what's the best way to maintain a mySQL connection throughout a session at a Web site. Currently I do something like the following..

    I have a db_connect() function in my header include and a db_disconnect() function in my footer include. However, sometimes I want to redirect the user with the header function. The problem then is that I don't disconnect to they mySQL database. I know mySQL is forgiving about this, however I would like to maintain connections and disconnections properly. Any suggestions? Thanks.

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP closes connections cleanly when a script terminates (this isn't MySQL being forgiving, it's PHP doing nice garbage collection for you). It is completely unnecessary to close database connections unless you do so during script execution (i.e. not at the end) as a means of reducing resource requirements.

    However, if you are dead set on doing your own cleanup, then take a look at register_shutdown_function. Or have a look at mysql_pconnect for persistent connections that don't close (they are reused instead of new ones opened when you make another mysql_pconnect call, so don't worry about that).
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pconnect can provide a small performance gain if you're in full control of the web and database servers. That way you can maintain an optimum number of connections.

    If you're on a shared host do not use pconnect. If everyone on the shared host uses it it'll often hurt database performance since too many connections will persist.

  4. #4
    SitePoint Enthusiast GhostGambler's Avatar
    Join Date
    Apr 2007
    Location
    Germany, NRW
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pconnections can be very evil with a config which is not prepared for those (and mysqls overhead to open a connection to a db is so low ... it's just not worth it to use pconnect)

    Actually, why do you just don't write a separate function for redirects?
    You could call header() and output some meta-refresh-site inside the function just in case, and just btw. close the connection to the database~

  5. #5
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone.

  6. #6
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One huge drawback of persistent connections is that you can't use transactions with them. Well, you can, but you may run into problems.

  7. #7
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    One huge drawback of persistent connections is that you can't use transactions with them. Well, you can, but you may run into problems.
    That's good to know, I was completely unaware of that problem! I don't use persistent connections anyway, but still it's good to be aware of such a potentially serious issue.
    PHP questions? RTFM
    MySQL questions? RTFM


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
  •