How can I use transactions in my MySQL stored procedure?
Solution 1:
Two syntax errors:
You need commas in between the conditions for your exit handler. Notice the syntax documentation shows commas.
You need to terminate the
END
of the exit handler with a semicolon. TheDECLARE
statement itself (including its BEGIN...END block) is a statement like any other, and need to have a terminator.
So you need this:
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
BEGIN
ROLLBACK;
END;
Solution 2:
Try like this ie, include your Declare
statement inside the START TRANSACTION;
. Earlier your ROLLBACK
was not a part of TRANSACTION
as you wrote it above the START TRANSACTION
:-
BEGIN
DECLARE poid INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
START TRANSACTION;
BEGIN
ROLLBACK;
END
-- ADD option 5
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,5,0);
SET poid = (SELECT LAST_INSERT_ID());
INSERT INTO product_option_value(product_option_id,product_id,option_id,option_value_id,quantity,subtract,price,price_prefix,points,points_prefix,weight,weight_prefix) VALUES(poid,insertedProductID,5,50,0,0,4.99,'+',0,'+',0,'+');
-- ADD option 12
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,12,1);
-- ADD option 13
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,13,0);
COMMIT;
END