Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistency in PDO transaction state #6355

Closed
wants to merge 2 commits into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Oct 20, 2020

This addresses an issue introduced by #4996 and reported in https://bugs.php.net/bug.php?id=80260.

Now that PDO::inTransaction() reports the real transaction state of the connection, there may be a mismatch with PDOs internal transaction state (in_tcx). This is compounded by the fact that MySQL performs implicit commits for DDL queries.

This PR fixes the issue by making beginTransaction/commit/rollBack work on the real transaction state provided by the driver as well (or falling back to in_tcx if the driver does not support it).

This does mean that writing something like

$pdo->beginTransaction();
$pdo->exec('CREATE DATABASE ...');
$pdo->rollBack(); // <- illegal

will now result in an error, because the CREATE DATABASE already committed the transaction. I believe this behavior is both correct and desired -- otherwise, there is no indication that the code did not behave correctly and the rollBack() was effectively ignored. However, this is still a BC break, so I wanted to double check this point.

If we don't do this change, I think we need to revert #4996 and not add full inTransaction() support for MySQL.

@nikic nikic mentioned this pull request Oct 20, 2020
@mvorisek
Copy link
Contributor

👍 from me

but what about other DB vendors? I think, the original PR:
https://github.com/php/php-src/pull/4996/files#diff-02177c92c94307ce53e201a823f1731aac9421e10896b335668ccb04b4107b1cR556
fixed only MySQL and at least Sqlite is unfixed:

NULL, /* in_transaction */

@nikic
Copy link
Member Author

nikic commented Oct 20, 2020

@mvorisek This change has no effect on database drivers that don't implement custom in_transaction reporting.

@othercorey
Copy link
Contributor

The original PR suggested the motivation was to allow mixing manual and PDO transaction commands. Should the test it added be adjusted to include scenarios like manually executing BEGIN then calling PDO::rollback()?

@nikic
Copy link
Member Author

nikic commented Oct 26, 2020

@othercorey Indeed, I've added some test coverage for that as well.

@php-pulls php-pulls closed this in 7b9519a Oct 26, 2020
SammyK added a commit to DataDog/dd-trace-php that referenced this pull request Nov 3, 2020
SammyK added a commit to DataDog/dd-trace-php that referenced this pull request Nov 3, 2020
SammyK added a commit to DataDog/dd-trace-php that referenced this pull request Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants