回滚到保存点

rollback to SAVEPOINT

我在 phpmyadmin 中尝试以下语句。数据库:mysql。

INSERT into cust values(5,'srk'); 
commit; 
UPDATE cust set cname='sk' where cid=5; 
savepoint A;

这些语句执行成功。

但是当我执行

rollback to A;

错误:

#1305 - SAVEPOINT A does not exist

错误来了。

如果我只执行回滚; 执行成功但结果实际上没有回滚

首先,您甚至没有参与交易。即使是 rollback to a savepoint 中的一次,您也必须承诺让它被看到。你只需要玩它。我希望这应该有所帮助。

一个人开始交易 start transaction;

create table cust
(   id int auto_increment primary key,
    theValue int not null,
    theText varchar(50) not null,
    cname varchar(50) not null,
    cid int not null
);

INSERT into cust (theValue,theText,cname,cid) values(111,'aaa','a',1); 


start transaction;
    savepoint B1;
    INSERT into cust (theValue,theText,cname,cid) values(666,'aaa','a',1); 
    savepoint B2;
    INSERT into cust (theValue,theText,cname,cid) values(777,'aaa','a',1); 
    ROLLBACK to B2;
    -- at this moment, this connection can see 2 rows, other connections see 1 (id=1)
    select * from cust; -- visible to you but not others, that is,
commit;
-- at this moment all connections can see 2 rows. Give it a try with another connection open

.

select * from cust;
+----+----------+---------+-------+-----+
| id | theValue | theText | cname | cid |
+----+----------+---------+-------+-----+
|  1 |      111 | aaa     | a     |   1 |
|  2 |      666 | aaa     | a     |   1 |
+----+----------+---------+-------+-----+

来自手册页 SAVEPOINT, ROLLBACK TO SAVEPOINT, and RELEASE SAVEPOINT Syntax

The ROLLBACK TO SAVEPOINT statement rolls back a transaction to the named savepoint without terminating the transaction.


重要的是要知道,在您的代码第 2 行 commit 中,您从未参与交易。你从来没有开始过。 commit.

什么都没有

您的第 1 行,插入,考虑到它不在事务中,是一个小型隐式事务。它只是发生了。当你的第 2 行出现时,服务器在想,提交什么?

必须要设置,

设置自动提交=0;