From:             ben dot pineau at gmail dot com
Operating system: Linux
PHP version:      5.2.11
PHP Bug Type:     PDO related
Bug description:  pdo_pgsql prepare() re-use previous aborted transaction

Description:
------------
When prepar()ing a statement in a separate function, pdo_pgsql behaves as
if we hadn't rollbacked and started a new transaction.

But no such problem when either :
- we use another pdo driver (tested with sqlite)
- prepare() call is inlined in the same code block as beginTransaction
(rather than in a distinct function)
- the function contains a straigth "query()" instead of prepare() +
execute()

Reproduced with both php-5.2.6 and php-5.2.11, PostgreSQL 8.3.8.


Reproduce code:
---------------
/*
 * CREATE TABLE test (a SERIAL PRIMARY KEY);
 * INSERT INTO test VALUES (1);
 */

$cnx = new PDO('pgsql:dbname=testbase;host=localhost', 'postgres','');
$cnx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO test (a) VALUES (1)";

function prepare_and_exec_query(&$pdo, $sql)
{
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
}

for ($i = 0; $i < 3; $i++) {
    try {
        $cnx->beginTransaction();
        prepare_and_exec_query($cnx, $sql);
        $cnx->commit();
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage() . "\n";
        if (true === $cnx->rollback()) echo "rollbacked ok\n";
    }
}


Expected result:
----------------
Error: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value
violates unique constraint "test_pkey"
rollbacked ok
Error: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value
violates unique constraint "test_pkey"
rollbacked ok
Error: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value
violates unique constraint "test_pkey"
rollbacked ok

Actual result:
--------------
Error: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value
violates unique constraint "test_pkey"
rollbacked ok
Error: SQLSTATE[25P02]: In failed sql transaction: 7 ERROR:  current
transaction is aborted, commands ignored until end of transaction block
rollbacked ok
Error: SQLSTATE[25P02]: In failed sql transaction: 7 ERROR:  current
transaction is aborted, commands ignored until end of transaction block
rollbacked ok

-- 
Edit bug report at http://bugs.php.net/?id=49985&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49985&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49985&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49985&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49985&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49985&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49985&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49985&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49985&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49985&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49985&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49985&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49985&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49985&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49985&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49985&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49985&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49985&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49985&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49985&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49985&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49985&r=mysqlcfg

Reply via email to