Database server auto-close

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

    Database server auto-close

    I just wondered if someone could clarify... I have a a script connecting
    with mysql_connect and in an include at end of page mysql_close

    If they script terminates before end of page ie. with an exit() or maybe
    trigger_error() , does php always disconnect the open'ed connection?

    I ask because PHP manual says yes, but I've seen some comments relating
    to PHP 4 saying it doesn't.

    Please can you tell me your experiences?
  • Chung Leong

    #2
    Re: Database server auto-close

    On Apr 30, 10:58 am, Tyno Gendo <you@localhostw rote:
    I just wondered if someone could clarify... I have a a script connecting
    with mysql_connect and in an include at end of page mysql_close
    >
    If they script terminates before end of page ie. with an exit() or maybe
    trigger_error() , does php always disconnect the open'ed connection?
    >
    I ask because PHP manual says yes, but I've seen some comments relating
    to PHP 4 saying it doesn't.
    >
    Please can you tell me your experiences?
    Yes, that applies to all resource handles opened during execution--
    database connections included. It's actually just one instance of a
    general phenomenon in PHP. When the last variable pointing to the
    resource handle goes out of scope, the handle is closed. For example,
    in the following function:

    function log($s) {
    $f = fopen("log.txt" , "wb");
    fwrite($f, $s);
    }

    the file will be closed automatically when the function returns, as $f
    goes out of scope.

    And the following is perfectly okay:

    if(mysql_connec t( ... )) {
    }

    Since the handle returned isn't saved, the connection will be closed
    immediately.

    When a script terminates--one way or another--all variables within it
    are destroyed, so all resource handles will be closed.

    Persistent connections are a different story--you can only close these
    by terminating the web server.

    As a side note: Don't wait till the end of the page before you close
    the database connection. That's a recipe for "Too many connections"
    errors. Read the necessary data from the database then close the
    connection before you output to the client.

    Comment

    • Sanders Kaufman

      #3
      Re: Database server auto-close

      Chung Leong wrote:
      As a side note: Don't wait till the end of the page before you close
      the database connection. That's a recipe for "Too many connections"
      errors. Read the necessary data from the database then close the
      connection before you output to the client.
      Could you expand on that?
      I can't think of a way that closing at the end would do that.

      Comment

      • Chung Leong

        #4
        Re: Database server auto-close

        On Apr 30, 8:08 pm, Sanders Kaufman <b...@kaufman.n etwrote:
        Chung Leong wrote:
        As a side note: Don't wait till the end of the page before you close
        the database connection. That's a recipe for "Too many connections"
        errors. Read the necessary data from the database then close the
        connection before you output to the client.
        >
        Could you expand on that?
        I can't think of a way that closing at the end would do that.
        Well, it's not that hard to understand. It takes time for the page to
        get sent to the client. Generally it's a lot longer than the time it
        takes to retrieve data from the database. A query might take 100
        millisec. Transferring a HTML page could conceivably take over 10
        seconds. Holding onto the connection til the end of the page means
        more concurrent connection there needs to be.

        Comment

        Working...