[PHP-DEV] [Discuss] Add PDO disconnect() and isConnected()

I would argue we should discourage their use
(deprecation is my dream!)

I am with you on the deprecation of persistent connections, as the implementation is awkward, but not without addressing the use case.

As you well know, PDO is an object which also optionally uses PHP persistent resources to facilitate connection re-use across PHP requests, a feature which I don’t believe has a parallel in object space. Perhaps > > therein lies a feature request, i.e. to allow an object to be made persistent in the same way a resource can.

I floated the above idea. Any inclination that would be worth pursuing?

… would be an interface to declare the persistent connection as defunct, such that upon subsequent PDO creation, a new connection will be formed, and the former will be closed. This is distinct from a disconnect or > reconnect procedure, and similar to failing a “liveness” check upon persistent PDO creation. Users could then effectively replace their persistent PDO in the same manner they would do so for a “regular” PDO.

This idea is growing on me, to address the issue of a defunct persistent connection, rather than the disconnect() interface. Any thoughts on introducing a new attribute to the PDO constructor to prompt the reconnection: PDO::ATTR_PERSISTENT_RECONNECT.

-Robert

Hi

On 10/1/25 16:45, AllenJB wrote:

As I understand it, at least in the case of MySQL, the state of
persistent connections is not managed by MySQL, but by the client (PHP).
This means that it's (at least theoretically) possible for a connection
to end up in an undesirable state, in which case there should be a way
to close it so it's not reused.

I have filed Send COM_RESET_CONNECTION in mysqlnd_conn_data::restart_psession · Issue #20225 · php/php-src · GitHub. I am not sufficiently knowledgeable about PDO, MySQLnd and MySQL itself to implement it myself, but it should not be particularly hard and I think it would greatly improve safety of using persistent connections with MySQL.

Best regards
Tim Düsterhus