tags:

views:

476

answers:

3

I'm porting an application from php to fastcgi (c). My host runs apache.

Since the fastcgi app would be running in a loop, I could open a mysql connection, and leave it open for all incoming requests. Is this recommended?

I think I've read about an equal number of opinions saying the connection is way more expensive than the request and it should be persistent, and on the other hand people claiming that the open connection is a resource hog and should be closed each time.

Which one of these is correct in my context?

+1  A: 

I hate to say such an obvious answer, but...have you tried both ways and compared them? It's pretty easy to change a connect() to a pconnect() and just attempt it with both methods. Do some profiling, especially under load, and see what works best on your combination of hardware and software.

On high-traffic sites, sometimes you can't get your DB to accept enough connections to allow persistent to work, but in general, persistent connections tend to be more efficient.

Ben Williams
A: 

I would say if you have atleast 10-20 people using ur site at all times than persistent connections work the best. anything less is overkill

w-ll
+1  A: 

It depends a lot on your setup. MySql is notoriously fast for establishing connection; You could say it was designed for that. If the database is on the same machine as the web-server, then it is really fast. You have to weigh this against the added complexity of keeping the connection alive. If each script create a new connection, a malbehaving script can't take the other scripts down. There are also issues such as per-connection state to take into consideration. So as a rule of thumb, I would say that unless you have identified the connection as a performance bottleneck, don't try to optimise on it.

troelskn