i2f-dw
Last Updated: April 12, 2018
·
2.841K
· shaybenmoshe

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/

3 Responses
Add your response

110

Great tip! Surprisingly I didn't even know this existed so thanks a ton.

over 1 year ago ·
209

@samuelm Thank you, didn't know that.

over 1 year ago ·
29815

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.

over 1 year ago ·