PDO Persistent Connection
PDO lets us open the connection as a persistent connection.
This means that rather than establishing a new connection with each request, the connection to the database is cached and reused.
Persistent connection consumes a lot less memory and CPU time.
Establishing a persistent connection is simple, just pass the relevant parameter in the initialization.
For example, instead of
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
use
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(PDO::ATTR_PERSISTENT => true));
A more complete analysis can be found here: http://blog.shay.co/pdo-persistent-connection-analysis/
Written by Shay Ben Moshe
Related protips
3 Responses
Note that this doesn't wroks if php run as CGI,
Persistent connections are only supported if you’re using PHP in the mod_php variety, and not the CGI wrapper.
@samuelm Thank you, didn't know that.
Apart from persistent connection, you can also easily perform CRUD operations in PDO. You can use PDO connection in PHP easily by using its connection class.