mysql transaction -在任何异常时回滚

jyztefdp  于 12个月前  发布在  Mysql
关注(0)|答案(3)|浏览(124)

如果mysql命令列表中出现任何错误,是否可以自动回滚?
例如,沿着以下路线的东西:

begin transaction;

insert into myTable values1 ...
insert into myTable values2 ...;  -- will throw an error

commit;

字符串
现在,在执行时,我希望整个事务失败,因此我应该NOT在myTable中看到values 1。但不幸的是,即使事务有错误,该表也会使用values 1。
有什么想法我怎么使它回滚?(再次,在任何错误)?
EDIT -从SQL更改为标准SQL

jtw3ybtb

jtw3ybtb1#

您可以通过以下方式使用13.6.7.2. DECLARE ... HANDLER Syntax

DELIMITER $$

CREATE PROCEDURE `sp_fail`()
BEGIN
    DECLARE `_rollback` BOOL DEFAULT 0;
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` = 1;
    START TRANSACTION;
    INSERT INTO `tablea` (`date`) VALUES (NOW());
    INSERT INTO `tableb` (`date`) VALUES (NOW());
    INSERT INTO `tablec` (`date`) VALUES (NOW()); -- FAIL
    IF `_rollback` THEN
        ROLLBACK;
    ELSE
        COMMIT;
    END IF;
END$$

DELIMITER ;

字符串
有关完整的示例,请查看以下SQL Fiddle。

wnavrhmk

wnavrhmk2#

例如,如果需要在代码中SIGNAL特定的SQL EXCEPTION,则可以使用EXIT HANDLER。例如:

DELIMITER $$

CREATE PROCEDURE `sp_fail`()
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        ROLLBACK;  -- rollback any changes made in the transaction
        RESIGNAL;  -- raise again the sql exception to the caller
    END;

    START TRANSACTION;
    insert into myTable values1 ...
    IF fail_condition_meet THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Custom error detected.', MYSQL_ERRNO = 2000;
    END IF;
    insert into myTable values2 ...  -- this will not be executed
    COMMIT; -- this will not be executed
END$$

DELIMITER ;

字符串

x8goxv8g

x8goxv8g3#

上述解决方案很好,但为了使其更简单,

DELIMITER $$

CREATE PROCEDURE `sp_fail`()
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        ROLLBACK;  -- rollback any error in the transaction
    END;

    START TRANSACTION;
    insert into myTable values1 ...
    insert into myTable values2 ...  -- Fails
    COMMIT; -- this will not be executed
END$$

DELIMITER ;

字符串

相关问题