Persistent connection support was introduced in PHP 5.3 for the
mysqli extension. Support was already present
in PDO MYSQL and ext/mysql. The idea behind persistent connections
is that a connection between a client process and a database can
be reused by a client process, rather than being created and
destroyed multiple times. This reduces the overhead of creating
fresh connections every time one is required, as unused
connections are cached and ready to be reused.
Unlike the mysql extension, mysqli does not provide a separate
function for opening persistent connections. To open a persistent
connection you must prepend
p: to the hostname
The problem with persistent connections is that they can be left in unpredictable states by clients. For example, a table lock might be activated before a client terminates unexpectedly. A new client process reusing this persistent connection will get the connection “as is”. Any cleanup would need to be done by the new client process before it could make good use of the persistent connection, increasing the burden on the programmer.
The persistent connection of the
extension however provides built-in cleanup handling code. The
cleanup carried out by
Rollback active transactions
Close and drop temporary tables
Reset session variables
Close prepared statements (always happens with PHP)
Release locks acquired with
This ensures that persistent connections are in a clean state on return from the connection pool, before the client process uses them.
mysqli extension does this cleanup by
automatically calling the C-API function
The automatic cleanup feature has advantages and disadvantages though. The advantage is that the programmer no longer needs to worry about adding cleanup code, as it is called automatically. However, the disadvantage is that the code could potentially be a little slower, as the code to perform the cleanup needs to run each time a connection is returned from the connection pool.
It is possible to switch off the automatic cleanup code, by
compiling PHP with
mysqli extension supports persistent
connections when using either MySQL Native Driver or MySQL