【问题标题】:issue with SavePoint in mysqlmysql中的SavePoint问题
【发布时间】:2011-05-31 21:35:11
【问题描述】:

我正在尝试使用 MySQL 中的保存点,但似乎出了点问题。

MySQL transaction conundrum

我收到如下所示的错误:

错误 1305 (42000):保存点 sp_prc_work 不存在

我的程序有或没有保存点的工作方式完全相同。我所期望的是值'4','pqr'不应该出现在表格中,因为 整个事务将被回滚。但是插入了 3 个和 4 个 ID。我明白为什么条目 '3', 'pqr' 在那里,但我猜 id '4' 不应该在那里。

drop table if exists test.savepoint_test;
drop procedure if exists second_fail;
drop procedure if exists prc_work;

CREATE TABLE test.savepoint_test (
id int not null default '0',
name varchar(100),
primary key (id)
)engine=InnoDB;

insert into test.savepoint_test values ('1', 'abc');
insert into test.savepoint_test values ('2', 'xyz');

select * from test.savepoint_test;

delimiter $$

CREATE PROCEDURE second_fail()
BEGIN
        INSERT  into test.savepoint_test values ('3', 'pqr');
        INSERT  into test.savepoint_test values ('2', 'mnp');
END;

$$

CREATE PROCEDURE prc_work()
BEGIN
        DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK TO sp_prc_work;
        SAVEPOINT sp_prc_work;
        INSERT  into test.savepoint_test values ('4', 'pqr');
        INSERT  into test.savepoint_test values ('2', 'mnp');
END;

$$

delimiter ;


call second_fail();

select * from test.savepoint_test;

call prc_work();

select * from test.savepoint_test;

【问题讨论】:

    标签: mysql


    【解决方案1】:

    改变这一行

        DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK TO sp_prc_work;
        SAVEPOINT sp_prc_work;
    

        SAVEPOINT sp_prc_work;
        DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK TO sp_prc_work;
    

    这应该可以解决问题,你是在告诉 mysql 回滚到一个不存在的保存点

    http://dev.mysql.com/doc/refman/5.0/en/savepoint.html

    直流

    我已经把你的例子改成了我认为你真正想要的

    请注意开始交易

    drop table if exists test.savepoint_test;
    drop procedure if exists second_fail;
    drop procedure if exists prc_work;
    
    CREATE TABLE test.savepoint_test (
    id int not null default '0',
    name varchar(100),
    primary key (id)
    )engine=InnoDB;
    
    delimiter $$
    
    CREATE PROCEDURE second_fail()
    BEGIN
            INSERT  into test.savepoint_test values ('3', 'pqr');
            INSERT  into test.savepoint_test values ('4', 'mnp');
    END;
    
    $$
    
    CREATE PROCEDURE prc_work()
    BEGIN
            SAVEPOINT sp_prc_work;
            BEGIN
                    DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK TO sp_prc_work;
                    INSERT  into test.savepoint_test values ('5', 'cat');
                    INSERT  into test.savepoint_test values ('2', 'dog');
            END;
            RELEASE SAVEPOINT sp_prc_work;
    END;
    
    $$
    
    delimiter ;
    
    START TRANSACTION;
    
    select 'test point 1' as ``;
    
    insert into test.savepoint_test values ('1', 'abc');      
    insert into test.savepoint_test values ('2', 'xyz');
    
    select * from test.savepoint_test;
    
    select 'test point 2' as ``;
    
    call second_fail();    
    
    select * from test.savepoint_test;
    
    select 'test point 3'  as ``;
    
    call prc_work();
    
    select * from test.savepoint_test;
    
    select 'test point 4' as ``;
    
    COMMIT;  
    

    直流

    【讨论】:

    • ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 'DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK TO sp_prc_work; 附近使用的正确语法; # 更改保存点行时出现的错误
    • 我需要在 BEGIN 和 END 子句中保存保存点。现在可以使用了,谢谢。
    • 发生了什么或没有发生什么。您是否收到某种错误消息?
    • 得到同样的错误 # ERROR 1305 (42000): SAVEPOINT sp_prc_work does not exist # 有没有办法检查存储过程中的保存点?
    • 谢谢。这是工作。我忘记了开始交易;并承诺;声明
    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 2016-12-02
    • 2014-05-11
    • 2011-06-15
    • 2018-03-18
    • 2013-04-30
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多