我正在用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
错误来了。
如果我只执行回滚;它执行成功,但结果实际上不会回滚。
发布于 2015-11-27 05:43:06
首先,你甚至没有参与交易。而且,即使在一个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语句将事务回滚到指定的savepoint,而不终止事务。
重要的是要知道,在代码的第2行,即commit中,您从未处于事务中。你从来没有开始过。commit什么都没有。
您的第1行insert (考虑到它不在事务中)是一个微型隐式事务。就这样发生了。当您的第2行出现时,服务器正在考虑,提交什么?
发布于 2020-11-20 06:17:57
你必须做好准备,
设置autocommit=0;

https://stackoverflow.com/questions/33950723
复制相似问题