One Global Connection vs. Open-Close

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

    One Global Connection vs. Open-Close

    All:

    I have a MS background and am having fun in the open source world of
    PHP and MySQL --- and I have a question.

    With PHP and MySQL, is it considered better practice to open a single
    database connection and re-use it throughout the life of the
    application (simple Web application - low traffic), or is it better to
    open a connection, use it and close it as needed?

    (With SQL Server, you'd do the latter, because connection pooling and
    other optimization mechanisms help ensure that connecting to the
    database is low cost.)

    Thanks,
    John
    jpuopolo

  • Vince Morgan

    #2
    Re: One Global Connection vs. Open-Close

    "john" <puopolo@gmail. comwrote in message
    news:1177851382 .764835.310600@ y80g2000hsf.goo glegroups.com.. .
    All:
    >
    I have a MS background and am having fun in the open source world of
    PHP and MySQL --- and I have a question.
    >
    With PHP and MySQL, is it considered better practice to open a single
    database connection and re-use it throughout the life of the
    application (simple Web application - low traffic), or is it better to
    open a connection, use it and close it as needed?
    >
    (With SQL Server, you'd do the latter, because connection pooling and
    other optimization mechanisms help ensure that connecting to the
    database is low cost.)
    >
    The latter is actualy very fast in php too. Surprisingly so.
    Vince


    Comment

    • rf

      #3
      Re: One Global Connection vs. Open-Close


      "john" <puopolo@gmail. comwrote in message
      news:1177851382 .764835.310600@ y80g2000hsf.goo glegroups.com.. .
      All:
      >
      I have a MS background and am having fun in the open source world of
      PHP and MySQL --- and I have a question.
      >
      With PHP and MySQL, is it considered better practice to open a single
      database connection and re-use it throughout the life of the
      application (simple Web application - low traffic), or is it better to
      open a connection, use it and close it as needed?
      Define "applicatio n".

      In the context of the stateless http protocol there is no "applicatio n".
      There are only individual seperate request to get a specific page at a
      specific URL.

      --
      Richard.


      Comment

      • Jerry Stuckle

        #4
        Re: One Global Connection vs. Open-Close

        john wrote:
        All:
        >
        I have a MS background and am having fun in the open source world of
        PHP and MySQL --- and I have a question.
        >
        With PHP and MySQL, is it considered better practice to open a single
        database connection and re-use it throughout the life of the
        application (simple Web application - low traffic), or is it better to
        open a connection, use it and close it as needed?
        >
        (With SQL Server, you'd do the latter, because connection pooling and
        other optimization mechanisms help ensure that connecting to the
        database is low cost.)
        >
        Thanks,
        John
        jpuopolo
        >
        (Hit send too fast)

        As for connection pooling - yes, I've seen this being recommended in SQL
        Server also - but I've found it's only good when you have a lot going on
        - same with PHP and MySQL.

        The reason is - with connection pooling, you are allocating server
        resources all the time - whether they are needed or not. And you have
        to allocate at least as many resources as you may *ever* need - and
        should allocate more, just as a buffer. So, fir instance, if you at
        some peak time you might need 200 connections concurrently, you need 200
        connections in the pool all the time.

        It's more important with SQL Server because it takes longer to connect
        to the server. MySQL is a lot more efficient in that respect, and while
        under certain conditions pooling (or persistent connections) might help,
        you won't find the gain you do with SQL Server.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • john

          #5
          Re: One Global Connection vs. Open-Close

          On Apr 29, 9:48 am, "rf" <r...@invalid.c omwrote:
          "john" <puop...@gmail. comwrote in message
          >
          news:1177851382 .764835.310600@ y80g2000hsf.goo glegroups.com.. .
          >
          All:
          >
          I have a MS background and am having fun in the open source world of
          PHP and MySQL --- and I have a question.
          >
          With PHP and MySQL, is it considered better practice to open a single
          database connection and re-use it throughout the life of the
          application (simple Web application - low traffic), or is it better to
          open a connection, use it and close it as needed?
          >
          Define "applicatio n".
          >
          In the context of the stateless http protocol there is no "applicatio n".
          There are only individual seperate request to get a specific page at a
          specific URL.
          >
          --
          Richard.
          My definition of an application in this context is: a set of Web pages
          and Web-based, server-side resources that act as a semantic or logical
          unit, whose state is tied together for some period of time using a
          session-based mechanism.

          Comment

          • Chung Leong

            #6
            Re: One Global Connection vs. Open-Close

            On Apr 29, 2:56 pm, john <puop...@gmail. comwrote:
            All:
            >
            I have a MS background and am having fun in the open source world of
            PHP and MySQL --- and I have a question.
            >
            With PHP and MySQL, is it considered better practice to open a single
            database connection and re-use it throughout the life of the
            application (simple Web application - low traffic), or is it better to
            open a connection, use it and close it as needed?
            >
            (With SQL Server, you'd do the latter, because connection pooling and
            other optimization mechanisms help ensure that connecting to the
            database is low cost.)
            >
            Thanks,
            John
            jpuopolo
            It's not so much a better practice as the only practice: In PHP, you
            can't create a connection that last beyond the current request. So you
            have to reestablish the connection everytime. There is this feature
            called persistent connection in PHP but it should be avoided like the
            plague. It has a way of leading to connection failures and the saving
            is negligible when the database is hosted on the same server.

            Even without connection pooling, one should close the connection as
            soon as it isn't need as there's a limit to the number of concurrent
            connections in MySQL.

            Comment

            Working...