PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

PHP MySQL persistent, cached, pooled connection - for how long?

Note: This article was originally published at Planet PHP on 19 April 2180.
Planet PHP

It is a common question: what is the lifespan of a persistent, pooled, cached, however-you-call-it PHP MySQL connection? What about CGI, FastCGI, web server module? The answer is always the same. Standard I/O file handles, including socket connections, are bound to processes. The lifespan of a persistent, pooled, cached, however-it-is-to-be-called-in-context-x connection is that of the PHP process. Depending on the web server deployment model, a PHP process handles one or multiple web requests. Read on only if puzzled.

A process

Do you have any colleguages around? Please, ask them for "APUX". In case of no reply, get a copy and study. To become a master PHP developer you will have to master todays frameworks (Zend Framework, Symfony, Dojo, a) and yesterdays. Yesterdays framework is still dominant. Yesterdays framework is your operating system. "APUX" stands for Advanced Programming in the UNIX Environment.

Building blocks of a process A Task state and identifier PID = 112, parent, children, a Credentials User-ID, group-ID, a Signal handler a Standard I/O Handles (descriptors) Memory Heap, stack, BSS, a a a

Standard I/O file handles, including socket connections, are bound to processes. The process may happen to execute the PHP interpreter (interpiler). PHP may open a socket connection to MySQL. If the process ends, the connection handle to MySQL goes away. Like the memory of a process is free'd, the connection handle is released as well.

A PHP process A a a Standard I/O Connection handle to MySQL a a

Webserver using CGI

If running PHP via CGI, the MySQL connection pooling/caching/persisting story pretty much ends here. For every (n = 1) web request the webserver starts and stops a process to handle the (n = 1) web request. Any process memory allocated is free'd at the end of the web request. Any standard I/O handle opened is closed. The lifespan of a MySQL connection is that of a single web request, a single script execution.

  1. Accept HTTP web request
    • Start worker process (PHP)
    • Execute *.php script through worker
    • Shutdown worker process
  2. Send worker's reply to client

A webserver using FastCGI

Once I talk FastCGI, I get questions. I used grey to highlight the differences. The difference is minimal.

  1. Accept HTTP web request
    • Start worker process (PHP), if none cached
    • Execute *.php script through worker
    • Shutdown worker process, if work limit exceeded
  2. Send worker's reply to client

For every n 1 web requests the webserver starts and stops a process to handle the (n 1) web requests. One PHP process handles n web requests. Process memory is available for the duration of n web requests. Standard I/O handles can be kept open for the duration of n web requests. A pooled/cached/persisted MySQL connection is such an I/O handle. It can be reused (at least) n - 1 times by a worker process.

Compare to CGI there is no difference but the value of n. Simple as that.

Web server FastCGI process a FastCGI process Sta

Truncated by Planet PHP, read more at the original (another 5273 bytes)