[php-src] PR #19214: PDO - Added PHP::disconnect and PHP::isConnected and refactored free object
| From: | rposky | Date: | Tue, 22 Jul 2025 17:01:59 +0000 |
| Subject: | [php-src] PR #19214: PDO - Added PHP::disconnect and PHP::isConnected and refactored free object | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-32572@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/19214
Author: rposky
The principal target of this PR is to introduce
PDO::disconnect() to disconnect from
the database as an alternative to PDO's destructor, which feature is related to bug
[#40681](https://bugs.php.net/bug.php?id=40681) and requests
[#62065](https://bugs.php.net/bug.php?id=62065) and [#67473](https://bugs.php.net/bug.php?id=67473).
Further and related efforts have been made herein to modify the destruct behavior of persistent PDO
db connections. The rollback invocation questioned with bug
[#63343](https://bugs.php.net/bug.php?id=63343) is postponed until time of disconnect as opposed to
PDO object destruct, given the potential to affect other PDO instances. Additionally, PDO object
integrity is maintained w.r.t. it's inner db handle in the case that a liveness check fails
upon other PDO construct and a new persistent resource is registered.
Current recommendations in PDO application integration avoid the need for an explicit disconnect
routine and workaround the above described issues through prescribed use of a single PDO object
instance for any given database connection, and which database connection is terminated upon either
PDO object destruct or, in the case of persistent connections, at the end of script execution. While
the use of a singleton object for database connection handling within applications is a common
implementation pattern, the tight integration of database disconnect behavior with PHP's object
free hook has imposed a need to further manage PDO object references within an application in order
to reliably replace and/or shutdown a db connection instance, an effort which scales with
application complexity. This reality is further complicated by the PDO statement object, which also
holds a reference to a PDO object and thus must also be managed from a reference standpoint, as well
as by PHP itself, which governs the time at which an object free hook is invoked, subject to
influence by bug or design. It is also worth noting that in the current form, it is not possible to
use the PDO object to determine the state of database connection closure, whether normal or errant.
The proposed changes add application integration options without majorly modifying current behavior,
but the following behavior changes are present.
- When a db connection has been closed through call to PDO::disconnect(), subsequent
invocations against the same db handle by any surviving PDO objects will throw a novel
PDOException with the message "connection is closed".
- The _pdo_object_t::is_closed struct member is put to use, presently not referenced
within the source. Other driver implementations which have used the struct member in unintended ways
may suffer.
- The _pdo_object_t::refcount struct member accurately reflects a count of PDO objects
referring to it, regardless of persistent connection state.
- Both PDO::errorInfo() and PDO::errorCode() may now be called on an
uninitialized PDO object, i.e. one without a defined driver member. This change is
being done to permit use of both functions after PDO::disconnect() is called, and as a
result of modifying the PDO_CONSTRUCT_CHECK macro to impose check on
is_closed.
- Do not rollback a persistent db connection when a PDO object is freed, but do rollback when a
persistent, and connected, db connection resource is freed at end of script execution.
- Do not free memory for a persistent db connection when its resource is destructed if still
referenced by PDO objects. This can occur when constructing a new PDO object and the current
persistent db connection fails a liveness check.
I trust you will inform me as this submission has departed from the contribution guidelines or
coding standards in any way that I may have missed, and welcome your feedback.