Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Workbench: Start Transaction seems to be committed before rollback

I wrote a bunch of delete statements and wrapped them in a transaction:

start transaction;
delete a...
delete b...
delete c...
rollback;

The idea is I would want the deletes to occur inside a transaction which would rollback upon completion. If a step failed along the way, I would want the successful steps to be rolled back as well.

To my chagrin delete a worked, removing a few thousand rows, delete b failed, but when I reran the statements all the records from a appeared to be gone.

Is this because the transaction is still open? I tried doing:

set session transaction isolation level read committed;
select a.*

and got back zero rows so I think that is not the case. Was the successful a delete committed? And if so, how do I prevent that from happening until I can guarantee a full working query?

like image 959
IcedDante Avatar asked Oct 17 '25 02:10

IcedDante


2 Answers

MySQL Workbench enables auto commit by default. In the SQL editor there is a toolbar button that can be used to toggle auto commit at will:

enter image description here

like image 95
Mike Lischke Avatar answered Oct 18 '25 20:10

Mike Lischke


Somebody turned me on to, what I think, is a better way to do this:

begin;
<sql transactions>
commit;
like image 33
IcedDante Avatar answered Oct 18 '25 20:10

IcedDante