Re: [PDO] Shared behavior of persistent connections

From: Date: Fri, 09 Sep 2016 06:23:51 +0000
Subject: Re: [PDO] Shared behavior of persistent connections
References: 1  Groups: php.pdo 
Request: Send a blank email to pdo+get-551@lists.php.net to get a copy of this message
Hi Christoph, I try to stay away as much as possible from persistent connections as they can cause more issues than what they actually solve. I think I even never used them on PDO. That said, yes that's the behaviour I would expect. Transactions should be rolled back and the environment should be completely clean from anything that's suppose to last for the current session only. Cheers On 02/09/2016 14:18, Christoph M. Becker wrote:
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>
-- Matteo Beccati Development & Consulting - http://www.beccati.com/

« previous php.pdo (#551) next »