Shared behavior of persistent connections
| From: | Christoph M. Becker | Date: | Fri, 02 Sep 2016 12:18:13 +0000 |
| Subject: | Shared behavior of persistent connections | ||
| Groups: | php.pdo | ||
| Request: | Send a blank email to pdo+get-550@lists.php.net to get a copy of this message | ||
Hi!
I wonder whether the shared behavior of persistent PDO connections is
really desired, as it is implemented now.
I stumbled upon this issue when I tried to resolve bug #63343[1] with PR
#2112[2]. Nikita pointed out the actual problem, namely that destroying
a PDO object will rollback the transaction on the inner object[3].
Digging a bit deeper brought up the issue that not only a transaction
would be rolled back, but also that the persistent_shutdown() method
will be called in that case. Consider the following script:
<?php
function foo() {return 1;}
$db1 = new PDO('sqlite::memory:', '', '',
array(PDO::ATTR_PERSISTENT => true));
$db1->sqliteCreateFunction('foo', 'foo', 0);
$st1 = $db1->query('select foo()');
echo $st1->fetchColumn();
//$db1 = $st1 = null;
$db2 = new PDO('sqlite::memory:', '', '',
array(PDO::ATTR_PERSISTENT => true));
$st2 = $db2->query('select foo()');
echo $st2->fetchColumn();
The output is
11. However, uncommenting the line which will force the
destruction of $db1, will also unregister the foo function, so the
second ::query() will fail. This can be quite confusing in a more
complex script.
Furthermore, in php_pdo_driver.h pdo_dbh_request_shutdown is
documented as[4]:
| called at request end for each persistent dbh; this gives the driver
| the opportunity to safely release resources that only have per-request
| scope
However, pdo_dbh_request_shutdown isn't just necessarily called only
at the end of the request, as shown above.
Anyhow, if the shared behavior of persistent connections works as
intented, the documentation should be updated respectively (and bug
#63343 closed as being not a bug).
[1] <https://bugs.php.net/bug.php?id=63343>
[2] <https://github.com/php/php-src/pull/2112>
[3] <https://github.com/php/php-src/pull/2112#discussion_r77312340>
[4]
<https://github.com/php/php-src/blob/PHP-7.0.11/ext/pdo/php_pdo_driver.h#L279-L282>
--
Christoph M. Becker