PDO’s ATTR_PERSISTENT attribute

· 228 words · 2 minute read

If you’ve ever used PDO and want to use persistent connections, here’s a tip: You must set PDO::ATTR_PERSISTENT => true in the array of options you pass to the PDO constructor!

If you call $pdo->setAttribute(PDO::ATTR_PERSISTENT, true) after the instantiation of the object, you won’t be using persistent connections.

To illustrate, here’s the correct way to use it:

$pdo = new PDO(
    'oci:dbname=foo',
    'username',
    'password',
    array(PDO::ATTR\_PERSISTENT => true));

Now, this makes perfect sense, but it’s not specifically called out in documentation. Granted, yes, the example here shows it being used in the constructor, but setAttribute() isn’t too chatty if you violate the unwritten “rules” of PDO. Only once you dig into the source code will you be able to figure this out (by virtue of the fact that the ATTR_PERSISTENT handling is only performed in the ctor!).

It’d be nice of PDO::setAttribute() raised a warning if you try to set PDO::ATTR_PERSISTENT, by the way. Maybe I’ll submit a patch or something.

Oh, also, if you’re using persistent connections, you can’t use a custom PDOStatement class. That was the nail in the coffin for us as we’re using a custom statement class. Somehow I think this may be fixable, though…sure would be nice.

Anyways, I still love you, PDO, but not as much as I did. Goodbye PDO (for now), hello oci8 with a wrapper class mimicking the PDO interface!