Hi Nikita,
On 12/04/2021 14:07, Nikita Popov wrote:
> $pdo->beginTransaction()
> $pdo->exec('DROP TABLE foobar'); // Implicitly commits transaction
> $pdo->exec('DROP TABLE barfoo'); // Transaction not active anymore
> $pdo->commit(); // Throws because no transaction active
>
> now throws an exception, because commit() is called without an active
> transaction. It's possible to use if ($pdo->inTransaction())
> $pdo->commit(); if you don't care about the lack of active transaction.
>
> I believe this behavior is correct, but I could see an argument made in
> favor of always allowing commit() calls (but not rollBack() calls) even if
> there is no active transaction. That would change the meaning of commit()
> from "commit an active transaction" towards "commit if an active
> transaction exists".
>
> Any opinions on that?
I agree. As a user I'd prefer to be alerted that the transaction was not
actually active rather than relying on seemingly working code that
doesn't do what's on the tin.
In fact I probably never knew about this particular MySQL quirk and most
likely I had been fooled many times into thinking that the code above
was a single transaction.
Cheers
--
Matteo Beccati
Development & Consulting - http://www.beccati.com/