我在用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
ROLLBACK TO SAVEPOINT语句将事务回滚到
命名保存点而不终止事务。
重要的是要知道,在代码的第2行commit中,您从未参与过事务。你从来没有开始过。没有什么可commit
您的第1行insert,考虑到它不在事务中,是一个小型隐式事务。只是碰巧。当你的第2行出现时,服务器在想,提交什么?

关于mysql - 回滚到SAVEPOINT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33950723/

10-10 23:36