473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Connection pooling in PHP ?

PHP Newbie here - I apologize if any of my questions appear daft or
obvious ...

Does PHP (or maybe the web server - Apache in my case), support
(database) connection pooling?. It seems terribly inefficient if every
request for data wil incurr the overhead of creating a connection to the
db.

While on the subject of pooling - does any one know (when using PHP in
"server side scripting") if scripts are launched as seperate processes
or if they are spawned as threads in a thread pool of a master process?
(pref the latter since it is less "expensive" in computer resource terms)

MTIA

Dec 20 '05 #1
4 14607
NC
Susan Baker wrote:

does any one know (when using PHP in "server side scripting")
if scripts are launched as seperate processes or if they are
spawned as threads in a thread pool of a master process?
Depends... PHP can be configured to run as a CGI executable (in which
case, if I remember correctly, each instance will be a separate
process) or as a module of an HTTP server, in which case PHP will
follow the threading model of the HTTP server. So if you run PHP as an
Apache (Apache 1.*, that is) module, each instance of Apache will start
as a separate process; if you run PHP as an ISAPI module under IIS, PHP
will run in threads...
(pref the latter since it is less "expensive" in computer resource
terms)


This may be, but it also means that a malfunction in one thread brings
down the entire process, including all other threads it has spawned...

Cheers,
NC

Dec 20 '05 #2

Susan Baker wrote:
Does PHP (or maybe the web server - Apache in my case), support
(database) connection pooling?. It seems terribly inefficient if every
request for data wil incurr the overhead of creating a connection to the
db.


Sure. Look at the pconnect() family of functions. The exact function
will depend on the database system you're connecting to. For MySQL
it's mysql_pconnect().

pconnect() will leave the connection open when done and reuse idle
connections if available. Be careful, though -- I've seen some weird
problems with connections not being properly closed, especially when a
query blocks.

Although it may seem inefficient to open a new database connection each
time, I'd try both ways (connect() and pconnect()) and see if you
really notice a difference. If you don't, it's probably worth sticking
to plain old connect().

Dec 20 '05 #3
>Does PHP (or maybe the web server - Apache in my case), support
(database) connection pooling?.
Take a look at pconnect(). However, be careful, it's not problem-free.
It seems terribly inefficient if every
request for data wil incurr the overhead of creating a connection to the
db.
If you leave a connection in a funny state, that connection when
allocated to another page can mess up that connection also.
A "funny state" is something that may interfere with pages that
expect a FRESH connection, and don't expect to have, say:
- autocommit turned off
- short timeouts that may have timed out before the new page started
- character set or collation set to something wierd
- sequence problems with a script aborting with a half-fetched
query, followed by another page starting with a new query

One of the problems with pconnect() and apache 1.3 is that you will
shortly end up with one connection per login per Apache child process.
If you routinely can have, say, a peak of 500 Apache processes going,
that may be more connections than your database will handle without
raising the defaults, which chews up more memory. This can be WAY
higher than the number of simultaneous *PHP* processes, depending
on what your webserver is also serving.

I'm not sure what the situation is with Apache 2, which uses threads.
You may have several connections using the same login info due
to demand for more while others are still in use.
While on the subject of pooling - does any one know (when using PHP in
"server side scripting") if scripts are launched as seperate processes
or if they are spawned as threads in a thread pool of a master process?


If PHP is run as a CGI, it's a separate process.
If PHP is run as a module, it depends on the process setup of
apache (in 1.3 it's a child process which may be re-used a hundred
times or so. In 2.* I think it's threads).

Gordon L. Burditt
Dec 20 '05 #4
Hello,

on 12/19/2005 11:39 PM Susan Baker said the following:
Does PHP (or maybe the web server - Apache in my case), support
(database) connection pooling?. It seems terribly inefficient if every
request for data wil incurr the overhead of creating a connection to the
db.
Apache supports connection pooling. Just set the MaxClients to a
reasonable limit. If there are more requests, clients will be enqueued.

Actually that is the smart way to achieve PHP database connection pooling.

If you move all static data to a separate server and keep only PHP
scripts that always use database connections in the same server, setting
Apache MaxClients achives the same effect as using a database connection
pooling middleware, except that it will be faster as there is no
middleware delay the communication with the database.

Here you can read more about this:

http://www.meta-language.net/metabas...ve-connections

While on the subject of pooling - does any one know (when using PHP in
"server side scripting") if scripts are launched as seperate processes
or if they are spawned as threads in a thread pool of a master process?
(pref the latter since it is less "expensive" in computer resource terms)


It depends on the Web server, but multi-threading servers are not
recommended with PHP because some extensions of PHP are not thread-safe.

--

Regards,
Manuel Lemos

Metastorage - Data object relational mapping layer generator
http://www.metastorage.net/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/
Dec 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Rob Nicholson | last post by:
We're getting an occasional occurrence of the following error when two users try and open the same record in our ASP.NET app: "There is already an open DataReader associated with this Connection...
1
by: Lenny Shprekher | last post by:
Hi, I am getting issues that Oracle collecting opened sessions (connections) from my webservice using regular System.Data.OleDb.OleDbConnection object. I am guessing that this is connection...
7
by: Mrinal Kamboj | last post by:
Hi , I am using OracleConnection object from Oracle ODP.net provider and following is the behaviour which i am finding bit strange : To start with my argument is based on followings facts : ...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
2
by: JimLad | last post by:
Hi, In an existing ASP/ASP.NET 1.1 app running on IIS 6, I need to RELIABLY pass the logged in username through to the SQL Server 2000 database for auditing purposes. The current method is...
16
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
3
by: fniles | last post by:
In the Windows application (using VB.NET 2005) I use connection pooling like the following: In the main form load I open a connection using a connection string that I stored in a global variable...
0
viswarajan
by: viswarajan | last post by:
Introduction This article is to go in deep in dome key features in the ADO.NET 2 which was shipped with VS 2005. In this article I will go trough one of the key features which is the Connection...
15
by: Sylvie | last post by:
I have a static function in a class, everytime I call this function, I am creating a SQLconnection, open it, use it, and null it, All my functions and application logic is like this, Every...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.