MySQL嵌套事务-未回滚

3

可能有重复:
MySQL允许嵌套事务吗?

我有一个存储过程,它使用事务,并在事务中调用另一个也使用事务并更新表的存储过程。第二个存储过程在循环内被调用,每次调用对单个行进行更新。这第二个存储过程还创建了一个临时表。 永久表的引擎为InnoDB,临时表为MyISAM。 MySQL版本为5.5.16。

如果出现错误,我想回滚第二个存储过程所做的所有更新。

这个可行吗?我知道DDL语句和start transaction;会发出commit,但有没有办法绕过它?

代码看起来像这样: (显然回滚不起作用)

delimiter $$
drop procedure if exists `proc1`$$
create procedure `proc1`( 
  ...#some variables
) 
modifies sql data

begin
    declare error int default 0;
    declare continue handler for sqlexception
    begin 
        set error=1;
    end;


     drop temporary table if exists table1;
     create temporary table table1 (

         id int unsigned, 
         col1 decimal(12,6) default 0, 
         col2 decimal (12,6) default 0, 
         col3 decimal (12,6),

         primary key (id)) engine=MyISAM;

     START TRANSACTION;
        begin
           declare id_1 int unsigned;
           declare v1 decimal(12,6) default 0;
           declare v2 decimal(12,6) default 0;
           declare v3 decimal(12,6) default 0;

           declare done int default 0;

          declare cur cursor for select id, col1, col2 from table1;
          declare continue handler for not found set done=1;

          begin
              open cur; 
              wh: while done=0 do
                  fetch cur into id, v1, v2;
                   if done=1 then
                          leave wh;
                   end if;

                   set v3=v1+v2 ;                                      
                   update table1 set col3=v3 where id =id_1;

                   CALL  proc2(id_1, v1, v2, v3);

                   end while wh;
                   close cur;
                   set done=0;
                 end;

            end;
           if error=0 then
                  commit;
                  set status=1;

            else 
                  rollback;
                 set status=-1;
            end if;


end$$
1个回答

6

每次只能打开一个事务。如果试图在已经存在事务的情况下启动新事务,则现有事务将被提交。但是,您可以使用保存点模拟嵌套事务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接