Linux运维MySQL存储事务的生命周期 老男孩培训周末

    /    2019-06-06

8、事务的生命周期(事务控制语句)

8.1 事务的开始

begin
说明:5.5 以上的版本,不需要手工begin,只要你执行的是一个DML,会自动在前面加一个begin命令。

8.2 事务的结束

commit:提交事务
完成一个事务,一旦事务提交成功 ,就说明具备ACID特性了。
rollback :回滚事务
将内存中,已执行过的操作,回滚回去

8.3 自动提交策略(autocommit)

db01 [(none)]>select @@autocommit;
db01 [(none)]>set autocommit=0;
db01 [(none)]>set global autocommit=0;
注:
自动提交是否打开,一般在有事务需求的MySQL中,将其关闭
不管有没有事务需求,我们一般也都建议设置为0,可以很大程度上提高数据库性能
(1)
set autocommit=0;   
set global autocommit=0;
(2)
vim /etc/my.cnf
autocommit=0   

8.4 隐式提交语句

用于隐式提交的 SQL 语句:
begin 
a
b
begin

SET AUTOCOMMIT = 1

导致提交的非事务语句:
DDL语句: (ALTERCREATE 和 DROP
DCL语句: (GRANTREVOKE 和 SET PASSWORD
锁定语句:(LOCK TABLES 和 UNLOCK TABLES
导致隐式提交的语句示例:
TRUNCATE TABLE
LOAD DATA INFILE
SELECT FOR UPDATE 

8.5 开始事务流程:

1、检查autocommit是否为关闭状态
select @@autocommit;
或者:
show variables like 'autocommit';
2、开启事务,并结束事务
begin
delete from student where name='alexsb';
update student set name='alexsb' where name='alex';
rollback;

begin
delete from student where name='alexsb';
update student set name='alexsb' where name='alex';
commit;


更多内容请关注微信公众号:oldboyedu

(0)

分享至