Hi,

I am trying to understand pros (if any) and cons with using a persistent db connection.
In the manual i have read that using persistent connection can speed up the application....
Are there ways to a priori determining if one could benefit or is a load measurement necessary?
I have read a few threads where the recommendation is to avoid persistent connections.
Maybe someone can elaborate around this matter.
I am planning on using the PDO classes and prepared statements.

Thanx

    Essentially persistent connections theoretically save work by keeping the connections around so they don't have to connect again. The pros are:
    - Saves a small amount of time connecting to the database each request.

    The cons are:
    - Leaves too many idle connections to your database in most cases
    - Creates difficult-to-reproduce bugs where the connections have some state associated with them

    In 99% of cases, the disadvantages are large.

    To fully understand, you must understand the shared-nothing model that PHP uses, and the model that your web server (e.g. Apache) uses to maintain its threads/processes.

    Assuming you use just a single connection to the database and you're using a dedicated server with no other applications on, each worker thread/process must maintain a single connection to the database all the time (in principle).

    This is often too many as people tune their web servers to high numbers of connections (MaxClients in Apache) and typically have several web servers.

    Normally in PHP, connections are made near to the beginning of the page, and held open until finished with or the page exits, when they are all closed.

    You haven't said what database you're using, but if it's MySQL, connecting is VERY fast under normal circumstances, so fast that it does not affect performance signficiantly.

    If you are at a point where you could possibly require persistent connections, you will have:

    • A performance / load test environment with real hardware and load driver(s)
    • A scheme for profiling your PHP application

    If you don't have at least both of those, don't even consider it.

    Mark

      Thanx Mark for your expert comment.
      I will then turn off persistent connection.
      I guess I have to RTFM to know how since for some reason I believe they are turned on now.
      I don't have to use any globals in my session handlers (yet)

        Write a Reply...