Additional information about database connections
PHP reuses connections, if possible. When a connection is being
made to the same Host with the same Username and Password as an
existing connection, no second connection is being made by PHP.
Instead the existing connection is returned to the caller. This
is true for both the *_connect() and *_pconnect()
calls of all PHP database interfaces.
This has implications for MySQL users: Never use the MySQL "use"
command to change the current database. If you do, session
management will fail to operate properly. Instead, create
all PHPLIB tables as part of your application.
Some databases (for example Oracle) have very expensive connect()
operations. For these databases, performance is dramatically improved
if you switch from CGI PHP to mod_php. This is because, for
these database, PHPLIB uses the persistent connection methods (e.g.
*plogon() or *_pconnect()) to connect to your database. In
mod_php, the database connection is kept around by the web
server process after the page has been processed and is reused if a
further connect requires a connection with the same
Host/Username/Password pattern.
This means that there will be at most "number of web server
processes" times "number of Host/Username/Password-combinations"
many simultaneous connections to your database server. Keep that in
mind when planning licenses and server load. Using CGI PHP and/or
non-persistent connections will probably reduce the number of
concurrent connects to your database server at the expense of
connection setup time. For database servers where connection setup
time is negligible (MySQL for example) this is the default solution;
just don't try it with Oracle.
Note: PHPLib now uses (as of 7.4) non-persistent
connections by default in the non-Oracle db interfaces. The majority
of PHPLib users are operating in a virtual hosting environment where
persistent connections eat up memory at a horrifying rate. For web
servers that only connect to one Host/Username/Password, the persistent
connections can be enabled by setting the $PConnect
variable in local.inc's database extension class.
Note: When using persistent connections with MySQL,
the wait_timeout value should be lowered in
the MySQL server (safe_mysqld). A recommended value of 120 and
180 seconds is much better suited to use with PHP than MySQL's default
value of 28800 seconds (8 hours).