I have to create a script that inserts some values into a transaction. Is it possible to roll back automatically if any error occurs on a list of mysql commands?
I have the following code so far:
SET autocommit=false;
START TRANSACTION;
INSERT INTO table(c1,c2)
VALUES('a','a');
INSERT INTO table(c1,c2)
VALUES('a','a'), -- should fail, duplicate entry
VALUES('b','b');
SELECT 'Success';
COMMIT;
ROLLBACK; -- Print message
SET autocommit=true;
I can't make it work for some reason first insert is always executed. any ideas how to make it to roll back on any error?