我在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
错误即将来临。
如果我只执行回滚; 它成功执行但实际上没有回滚结果。
答案 0 :(得分:2)
首先,你甚至没有参与交易。甚至一次为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行,插入,考虑到它不在事务中,是一个迷你隐式事务。它刚刚发生。当第2行出现时,服务器正在思考,提交什么?
答案 1 :(得分:0)
您必须设置,
set autocommit = 0;