Frage

For a MySQL stored procedure that uses a transaction, is it necessary to call ROLLBACK when exiting early?

CREATE PROCEDURE `Proc`(IN `param` VARCHAR(10))
sp:BEGIN
    START TRANSACTION;
    IF ( bad condition ) THEN
        ROLLBACK;     /* is this necessary? */
        LEAVE sp;
    END IF;
    ...
    COMMIT;
END
War es hilfreich?

Lösung

It is necessary, because the transaction is still open since you haven't ended it. Anything that implicitly commits the transaction (for example, starting a new transaction) will act as though you ran COMMIT, which is the opposite of what you want

edit It may be different for a stored procedure, since this normally kills everything that was in reference to it when it finishes, but it's always better to be safe than sorry. Also, explicitly killing it makes things easier to understand.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top