SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: which is best

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2000
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    which is best

    Which is best mysql_connect or mysql_pconnect, for a script which does alot of database querys each day from different people

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do believe the major difference is that with mysql_connect() it opens a connection to the db server. And that connection closes once the script ends or mysql_close() is called. mysql_pconnect() opens a persistent connection that doesn't close when the script ends unless mysql_close() is called. Also with mysql_pconnect(), before php opens a new connection it will look for an open first with the same host, user and pass info.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So then which would be best for the server using a heavily used script? I'm guessing mysql_pconnect(), right? Also, does using an open connection like this create any security holes?
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to add a little more of what I have read on the matter.

    Firstly, persistent connections must be turned on in the php.ini file for a persistent connection to be used.

    Secondly, you can safely call mysql_pconnect() without knowing whether persisten connections are enabled/disabled because, if they aren't then php will use a regular connection. So there is no worries there.

    Finally, (and this is only from what I have read - not from direct experience), I read a lot of posts to bulletin boards about people on shared servers compaining that they are using persistent connections yet they are often getting a "cannot connect to mysql server - too many connections" (or whatever it is) error. This is not a problem with php but with the apache configuration. Apache is a multi-process server. So each time a request comes in Apache opens a new child process until the maximum number of child processes are reached (as specified in the httpd.conf file). Each child process is kept alive for a certain time (also specified in the httpd.conf file). Now when php uses a persistent connection to mysql that connection remains alive for the life of the Apache child process. So you can see that if there are lots of child processes running with long time-outs set then the maximum number of connections to the mysql server could be reached when in fact they are not being used and are just idle.

    I post the above because that is my understanding of things. Please correct me if I am mistaken, as this is something I want to understand

  5. #5
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid
    Now when php uses a persistent connection to mysql that connection remains alive for the life of the Apache child process. So you can see that if there are lots of child processes running with long time-outs set then the maximum number of connections to the mysql server could be reached when in fact they are not being used and are just idle.
    So if I were to use a persistent connection, would Apache create a child-process ONCE dedicated to the specific script for each time its accessed? Or, would it create a new child-process for each user who uses the script?
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, this is the sort of thing I would like to have clarified by an Apache expert. I am guessing that apache will try and use an existing and idle child process (ie, one that is already running and has not reached its time-out life) for each request, if there is a child process already running that is free. In this case, if that child process opened a persistent connection to the mysql server while processing an earlier request, then the database connection is already in place for the current request. So my understanding is that for a very busy site (or a heavily loaded shared server) where the maximum number of child processes allowed might be reached, then the mysql server has to be running set to allow a similar number of connections or else all the available connections will be used up. That's if I understand things correctly. I've read the manual about this but it still leaves me a little foggy http://www.php.net/manual/en/feature...onnections.php

    This has probably moved off from the original topic of the thread. Sorry!

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Balto, MD
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Apache and Persistence

    HTTP is stateless.

    Apache starts as many child processes as needed to handle the incoming demand.

    For example, if fifteen people CONCURRENTLY requested a given page (or within a few seconds of one another), apache could open as many as fifteen child processes. Or whatever was specifically needed to handle the demand at a given time.

    However, it has been my experience that if fifteen people make requests one after the other, (with the second coming immediately after the first is completed, and so forth) apache would only really need to have ONE child process active.

    Since HTTP is stateless, each connection request is just like the other. The child process only stays active as long as is necessary to deliver the requested data.

    The default timeout on connections is 5 minutes with apache. You could shorten that interval (at your own peril) if you want. However, remember that that poor schlepp with the 14,400 bps modem will be negatively effected if you have large graphics and such. (In such a case, the connection may time out before all of the data is transmitted)

    Hope this helps.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Balto, MD
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Errata

    Since HTTP is stateless, each connection request is just like the other. The child process only stays active as long as is necessary to deliver the requested data.

    In the second sentence, I inadvertently said child process when I meant to say connection. The child processes stay alive a long time.


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
  •