php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66529 PDO::commit returns true on failure
Submitted: 2014-01-21 10:16 UTC Modified: 2020-10-28 14:50 UTC
Votes:6
Avg. Score:4.5 ± 0.8
Reproduced:5 of 5 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (20.0%)
From: fk at florian-kaiser dot net Assigned:
Status: Duplicate Package: PDO MySQL
PHP Version: 5.5.8 OS: Debian Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: fk at florian-kaiser dot net
New email:
PHP Version: OS:

 

 [2014-01-21 10:16 UTC] fk at florian-kaiser dot net
Description:
------------
When you use PDO::beginTransaction(), do a query and issue PDO::commit(), the result of the last call is bogus at best. 

Documentation states "Returns TRUE on success or FALSE on failure.". In current implementation, this is wrong. In fact, it returns TRUE in all cases I tested - even for _impossible_ commits. The only time it actually fails is when issuing a PDO::commit() while no transaction is active. This will, regardless of what you set PDO::ATTR_ERRMODE to, result in a PDO::EXCEPTION (that is another bug, see #63546).

Unfortunately, there is another bug in the implementation of PDO::commit() that makes it almost impossible to detect that error since "MySQL server has gone away" or "Error reading result set's header" are only issued as as warnings and neither PDO::errorCode() nor PDO::errorInfo() are set, nor is a PDOException thrown. PDO_ATTR_ERRMODE is also not beeing honored. I filed that bug under #66528 and will only focus on the bug described above in this report.

The easiest way to reproduce is to start a transaction, issue a query, sleep for a couple of seconds and while doing that, shutting down the MySQL-Server. When the server is clearly gone, issue PDO::commit(). Although PDO knows it can under no circumstances have commited the data, it returns success for the operation.

Test script:
---------------
<?php
$dbh = new PDO('mysql:dbname=test;host=127.0.0.1;charset=UTF8', 'testuser', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);

// prepare table for test
$dbh->query('DROP TABLE IF EXISTS importantdata');
$dbh->query('create table test.importantdata (a int) engine=innodb');

$dbh->beginTransaction();
$dbh->query('insert into importantdata (a) VALUES (1), (2)');

sleep(20); // shut down mysql-server

var_dump($dbh->commit());

?>

Expected result:
----------------
bool(false)

Actual result:
--------------
bool(true)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-03 08:28 UTC] mbeccati@php.net
-Package: PDO Core +Package: PDO MySQL
 [2014-11-03 08:28 UTC] mbeccati@php.net
Seems to be mysql-only. With PDO_pgsql I got:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 7 server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.'
 [2020-10-28 14:50 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2020-10-28 14:50 UTC] nikic@php.net
This is effectively the same issue (just for a different error mode) as bug #66528, which is now fixed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC