I want to test a script what it does when MySQL transaction fails. So I want to simulate transaction failure on COMMIT
.
Image the script opens a transaction, makes some queries, and then commits the transaction. I want to implant some extra query to the transaction, to ensure the transaction will fail on COMMIT
. So I can test how the script recovers from the transation failure.
I don't want to use explicit ROLLBACK
. I want to simulate some real-life case when a commit fails. The exact DB structure is unimportant. It may adapt to the solution.
Edit: I need the transaction to fail on COMMIT
, not on some prior query. Therefore answers that rollback prior to COMMIT
are not what I want. Such as this one: How to simulate a transaction failure?
My unsuccessful attempts:
Inserting a row with invalid PK or FK fails immediately with insert. Temporarily disabling FK checks with
FOREIGN_KEY_CHECKS=0
won't help as they won't be rechecked onCOMMIT
. If it was psql, defferable constraints would help. But not in mysql.Opening two parallel transactions and inserting a row with the same PK (or any column with unique constaint) in both transactions locks the later transaction on insert and waits for the former transaction. So the transaction rolls back on insert not on commit.
COMMIT
can actually fail at all in MySQL. – martin.macko.47COMMIT
to fail only if I killed mysql server before theCOMMIT
. But that's not very convenient for automated tests :-/ – martin.macko.47