tags:

views:

73

answers:

4

My understanding is that PHP's p* connections is that it keeps a connection persistent between page loads to the service (be it memcache, or a socket etc). But are these connections thread safe? What happens when two pages try to access the same connection at the same time?

A: 

Generally speaking, when a PHP script requests a persistent connection, PHP will look for one in the connection pool with the same connection parameters.

If one is found that is NOT being used, it is given to the script, and returned to the pool at the end of the script.

gahooa
What about running PHP on Windows, on which you are always using a threaded version ? Same on UNIX with Apaches worker MPM, btw.
Pascal MARTIN
A: 

The manual's page Persistent Database Connections might get you a couple of informations about persistent connections.

It doesn't say anything specific about thread safety, still ; I've quite never seen anything about that anywhere, as far as I remember, so I suppose it "just works OK". My guess would be a connection is re-used only if not already used by another thread at the same time, but it's just some kind of (logical) wild guess...

Pascal MARTIN
+8  A: 

In the typical unix deployment, PHP is installed as a module that runs inside the apache web server, which in turn is configured to dispatch HTTP requests to one of a number of spawned children.

For the sake of efficiency, apache will often spawn these processes ahead of time (pre-forking them) and maintain them, so that they can dispatch more than one request, and save the overhead of starting up a process for every request that comes in.

PHP works on the principle of starting every request with a clean environment; no script variables persist between page loads. (Contrast this with mod_perl or python, where applications often manifest subtle bugs due to unexpected state hangovers).

This means that the typical resource allocated by a PHP script, be it an image handle for GD or a database connection, will be released at the end of a request.

Some resources, particularly Oracle database connections, have quite a high cost to establish, so it is desirable to somehow cache that connection between dispatched web requests.

Enter persistent resources.

The way these work is that any given apache child process may maintain a resource beyond the scope of a request by registering it in a "persistent list" of resources. The persistent list is not cleaned up at the end of the request (known as RSHUTDOWN internally). When you use a pconnect function, it will look up the persistent list entry for a given set of unique credentials and return that, if it exists, or establish a new connection with those credentials.

If you have configured apache to maintain 200 child processes, you should expect to see that many connections established from your web server to your database machine.

If you have many web servers and a single database machine, you may end loading your database machine much more than you anticipated.

With a threaded SAPI, the persistent list is maintained per thread, so it should be thread safe and have similar benefits, but the usual caveat about PHP not being recommended to run in threaded SAPI applies--while PHP is itself thread safe, so many libraries that it uses may have thread safety problems of their own and cause you a good number of headaches.

Wez Furlong
<No Comment>
Chacha102
Ok, the guy is a PHP core dev, that explains the quality of the answer...
e-satis
A: 

Generally speaking, PHP will make one persistent connection per process or thread running on the webserver. Because of this, a process or thread will not access the connection of another process or thread.

Instead, when you make a database connection PHP will check to see if one is already open (in the process or thread that is handling the page request) and if it is then it will use it, otherwise it will just initialize a new one.

So to answer your question, they aren't necessarily thread safe but because of how they operate there isn't a situation where two threads or processes will access the same connection.

codeincarnate