#1
  1. Advisor
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Stockholm, Sweden
    Posts
    446
    Rep Power
    15

    mysql_close, how important?


    How important is it to close your mysql connection....

    I've done some PHP/Mysql programming but never closed
    my connections to mysql...
    I have never seen any lack of perforamnce or memory "eaten" up.

    I know in ASP against MS Access it's really important to close the database connections after use....
  2. #2
  3. Wiking
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    3,608
    Rep Power
    27
    I suppose it's good programming style to close connections when you're done...
    But as far as I've experienced, you only benefit from it if you have many different connections/querys etc in the same script or have many concurrent connections (ie heavy load). When you jump to another page mysql drops your connection, and you start a new one if that's in the new page. This is if you use mysql_connect().

    With mysql_pconnect(), I think it's better to close the connections when they're no longer necessary. But I try to avoid that kind of connection, so I'm not sure. But I'm sure that there are other people here who have better knowledge of this, and they'll probably tell us...

    And I think that the problem with connections in ASP lies within its session managment.

    /NoXcuz
    UN*X is sexy!
    who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount; gasp; yes; uptime; umount; sleep
  4. #3
  5. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    noxcus is right as far as when it's necessary. i.e. rarely.

    You can't close persistent connections at all. They survive for the life to the process... such as the apache child that calls the script. Obviously if you aren't using PHP as a module then a truly persistent connection cannot exist since the process that made the connection will cease to exist once the script is finished.
  6. #4
  7. Advisor
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Stockholm, Sweden
    Posts
    446
    Rep Power
    15
    Whe do you/I want to use persistent connections?

    what's the difference between mysql_connect() and
    mysql_pconnect()...?
  8. #5
  9. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    mysql_connect() opens a connection that exists for the duration of the script creating the connection or until mysql_close() is called.

    mysql_pconnect() opens a connection that exists for the duration of the PROCESS that creates the connection. mysql_close() has no affect on persistent connections. For example, using Apache you have several configuration directives that control how many child processes hang around waiting for requests and how many requests they will handle. Each of these children consume resources and mysql connections consume resources, that is each connection requires a seperate mysqld process to handle its requests.

    This means that if you have 100 apache children running and each of them ran a script that had a mysql_pconnect() at some point then you would have 100 mysql daemons running as well. That's a lot of memory being consumed.

    The benefit with persistent connections is that the CPU overhead of opening a connection is high, so a persistent connection avoids that at the cost of memory.

    All this put together means that which you should use depends on several things: CPU speed, RAM available, the ratio of static html requests PLUS the number of non database using scripts to the number of scripts requiring a db connection AND the number of Apache child processes you need to have to handle the load of requests you receive.

    One solution that can be used when you run into a wall where you have complete control of the machine you are using is to install a smaller http server (such as thttpd) with a seperate IP address that handles all static content for your site (html, images, js, css etc) and use Apache only for PHP pages. This would allow you to maintain persistent connections with fewer Apache children. Or use a seperate server all together for those requests.

    However, should your site use almost exclusively dynamic, db driven pages with few images and you don't have enough RAM to maintain the number of children you need then you can either not use persistent connections (if you have sufficient extra CPU cycles) or you have to upgrade your hardware.

    Of course, another solution would be to place mysql on another server.
  10. #6
  11. Advisor
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Stockholm, Sweden
    Posts
    446
    Rep Power
    15
    Thanks alot rod k!

IMN logo majestic logo threadwatch logo seochat tools logo