MySQL如果没有更新,我是否需要ROLLBACK事务

时间:2018-03-07 13:20:53

标签: mysql stored-procedures transactions commit rollback

我有以下存储过程。

我的数据库引擎是InnoDB,自动提交功能已开启。

在最后的ELSE语句中,我是否需要提供ROLLBACK;或者在存储过程返回后自动生成ROLLBACK。

CREATE DEFINER=`root`@`%` PROCEDURE `ApplyRoomPriceUpdate`(iRoomId INTEGER, dStartDate DATE, dEndDate DATE, dcPrice DECIMAL(10,4))

BEGIN
DECLARE iRoomCount INTEGER; 

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
    -- ERROR
    GET DIAGNOSTICS CONDITION 1 @p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
    SELECT 1 AS 'ReturnCode', @p1 AS 'MySqlErrNo', @p2 AS 'MySqlErrMsg';    
    ROLLBACK;
END;

DECLARE EXIT HANDLER FOR SQLWARNING
BEGIN
    -- WARNING
    GET DIAGNOSTICS CONDITION 1 @p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
    SELECT 2 AS 'ReturnCode', @p1 AS 'MySqlErrNo', @p2 AS 'MySqlErrMsg';    
    ROLLBACK;
END;

START TRANSACTION;

SELECT COUNT(RoomId) FROM Room WHERE RoomId = iRoomId INTO iRoomCount;

IF iRoomCount > 0 THEN

    UPDATE RoomDefault SET Price = dcPrice WHERE RoomId = iRoomId;

    COMMIT;

    SELECT 0 AS 'ReturnCode', NULL AS 'MySqlErrNo', 'Update applied' AS 'MySqlErrMsg';

ELSE
    -- FAILURE 
    SELECT 2 AS 'ReturnCode', NULL AS 'MySqlErrNo', 'Update failed' AS 'MySqlErrMsg';
    -- DO I NEED ROLLBACK HERE??
END IF;

END

0 个答案:

没有答案