mysql - 回滚到保存点

标签 mysql rollback

我在 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

错误来了。

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

最佳答案

首先,您甚至没有参与交易。即使是一次回滚到保存点,您也必须提交以使其可见。你只需要玩它。我希望这应该有所帮助。

使用 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,您从未参与过事务。你从来没有开始过。没有什么要提交

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

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

相关文章:

php date() 与 mysql MONTH()

git - 获取 mvn 版本 :rollback to delete tag in GIT

c++ - 自动提交被禁用并且永远不会回滚会发生什么?

tfs - TF204000 : The Team Foundation server to which your team project is connected does not support the Rollback command

MySQL SP 和事件被 Google Cloud SQL 自动回滚

php mysql - 在第三个结果后回显清除除法

MySQL - 将连接行值作为选择字段传递

php - MySQLi multi_query 不工作

java - 简单的流程回滚问题

mysql - 子查询获取最后一条记录