MySQL connection, when to close, when to connect...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sims

    MySQL connection, when to close, when to connect...

    Hi,

    I am trying to tidy up my code and now i am looking at my database work.
    Now correct me if i am wrong, (I am using mysql_connect).

    Lets assume a few functions...

    //////////////////////////////////
    // code
    /////////////////////////////////
    function A()
    {
    // connect
    // run a SELECT query
    // close
    }

    function B()
    {
    // connect
    // run a SELECT query
    }

    Now according to the documentation A() and B() are the same because the
    database is closed at the end of the function.
    So in function B() the close in implied. Right?

    But now lets assume within another function that i do many selects

    function testDB()
    {
    B();
    B();
    B();
    B();
    // close the id
    }
    now surely connecting and closing all the time is very bad for the
    performances.

    So should i save the connection ID to make sure that the DB only connects if
    it is not open?
    Something like...

    ///////////////////
    // code
    //////////////////

    $link = 0;
    function A()
    {
    global $link;
    if( $link == 0){
    $link = // connect
    }
    // run a SELECT query
    }

    The case above would be OK if the function does not close the connection
    when it exits?
    Should i rather do

    // some php
    $link = // connect;
    function A()
    {
    global $link;
    // if not connected then big problem...
    // run a SELECT query
    }

    function CloseA()
    {
    // close $link;
    }

    What would be the best way to do it?
    Many thanks

    Sims


  • Seb

    #2
    Re: MySQL connection, when to close, when to connect...

    Hi,

    According to me, the connexion is not closed at the end of the function,
    but at the end of the script.

    I'm using Oracle, but I don't think it's very different.

    I've built an abstraction class to manage the database connections and
    queries.

    I use one method to open the connection and instantiate my objects (that
    is my connexions), then other methods to perform queries.

    That gives :

    Connect = new DB();
    Connect->select(....) ;

    With Oracle we also have persistant connections using pconnect : PHP
    manage database connexions and don't close them, they are re-used when
    needed. I don't know if it exists with MySQL.

    Of course opening a new connection for each request in the same script
    is really not good ;-). I wouldn't be your database server when traffic
    will increase on your site ;-)

    Seb

    Comment

    • RaveRod

      #3
      Re: MySQL connection, when to close, when to connect...

      MySQL does support persitent connections.

      I have an abstraction layer written myself as well (to support multiple
      databases). I generally pass another variable indicating if a persistent
      connection is needed. That way, you can choose (when you create the object)
      to have persistent connections in your script or not.


      Comment

      Working...