I want to write a stored procedure:
Involving the data update of the two tables, it belongs to a transaction, and it is hoped that there will be a rollback if there is a problem in any place in the transaction (a problem is an exception or an update failure).
Problems encountered now:
I use ROW_COUNT() to monitor the execution of the second UPDATE statement. But this operation has some shortcomings. First: There is a problem with the first UPDATE statement, and I will not roll back immediately. Second: If there is a problem in the first sentence and the second sentence is executed normally, no rollback will occur. Neither of these is what I hoped for.
Current own solution ideas:
First: do a monitor under each UPDATE statement, but the shortcomings are very obvious. If there are many operations involved in my stored procedure in the future, the above solution is not very suitable. Second: monitor the exceptions that occur in the transaction, and deal with them uniformly (DECLARE EXIRHANDLER FOR SQLEXCEPTION ROLLBACK;). But this method also has a problem. For example, the condition in the WHERE in my second UPDATE statement is not met, and the number of affected rows is 0, but no exception will be thrown.
Would you like to ask everyone, is there any solution?The following is the stored procedure I wrote
CREATE DEFINER=`dev`@`%` PROCEDURE `sp_buyScore`(IN userID int, IN moeny int)
BEGIN
DECLARE original_balance int;
START TRANSACTION;
SELECT balance.`balance` FROM balance WHERE balance.`UserID` = userID INTO original_balance;
IF original_balance-moeny < 0 THEN
signal sqlstate '45000' SET message_text = 'Insufficient balance, unable to purchase ';
ELSE
UPDATE balance SET balance= balance - moeny WHERE balance.`UserID` = userID;
UPDATE userScore SET Score= Score + moeny * 10 WHERE userScore.`UserID` = userID;
IF ROW_COUNT() = 0 THEN
ROLLBACK;
signal sqlstate '45000' SET message_text = 'Update failed ';
END IF;
END IF;
COMMIT;
END