事務(wù):一個(gè)或一組SQL語(yǔ)句組成的一個(gè)執(zhí)行單元,要么全部成功,要么全部失敗。
注:mysql中只有innodb支持事務(wù)
默認(rèn)情況下mysql的事務(wù)是自動(dòng)提交的
show variables like '%autocommit%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
#需要手動(dòng)的禁用一下事務(wù)自動(dòng)提交
set autocommit = 0;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
#mysql8中查看隔離級(jí)別
select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
#mysql8之前查看隔離級(jí)別
select @@tx_isolation;
#修改全局隔離級(jí)別
set global transaction isolation level read committed;
#修改會(huì)話隔離級(jí)別
set session transaction isolation level read committed;
臟讀和幻讀相似,臟讀屬于修改操作,幻讀屬于插入操作
開(kāi)啟兩個(gè)mysql連接,為mysql1和mysql2
原始數(shù)據(jù)為
select * from users;
+----+---------+
| id | u_name |
+----+---------+
| 2 | 張三 |
| 3 | 張三0 |
| 4 | 張三1 |
| 5 | 張三2 |
+----+---------+
#首先設(shè)置mysql1和mysql2隔離級(jí)別為read uncommited
set session transaction isolation level read uncommitted;
#禁用mysql1 mysql2的自動(dòng)提交
set autocommit = 0;
update users set u_name='李四' where id = 3;#① mysql1修改數(shù)據(jù)但未提交
select * from users where id =3;#② mysql1 查詢(xún)數(shù)據(jù) 為李四
select * from users where id =3;#③ mysql2 查詢(xún)數(shù)據(jù) 也為李四 查詢(xún)到了mysql1中未提交的數(shù)據(jù) 出現(xiàn)了臟讀
insert into users (u_name) values ('劉亦菲');#④mysql1 插入數(shù)據(jù)但未提交
select * from users where u_name = '劉亦菲';#⑤mysql1 查詢(xún)到該數(shù)據(jù)
select * from users where u_name = '劉亦菲';#⑥mysql2 查詢(xún)到該數(shù)據(jù) 出現(xiàn)了幻讀
rollback;#⑦mysql1 回滾之前的操作 此時(shí)mysql2在查詢(xún)發(fā)現(xiàn)之前查詢(xún)到的數(shù)據(jù)都變了 出現(xiàn)了不可重復(fù)讀
#首先設(shè)置mysql1和mysql2隔離級(jí)別為read commited
set session transaction isolation level read committed;
#禁用mysql1 mysql2的自動(dòng)提交
set autocommit = 0;
update users set u_name='李四' where id = 3;#① mysql1修改數(shù)據(jù)但未提交
select * from users where id =3;#② mysql1 查詢(xún)數(shù)據(jù) 為李四
select * from users where id =3;#③ mysql2 查詢(xún)數(shù)據(jù) 為張三0 沒(méi)有出現(xiàn)臟讀
insert into users (u_name) values ('劉亦菲');#④mysql1 插入數(shù)據(jù)但未提交
select * from users where u_name = '劉亦菲';#⑤mysql1 查詢(xún)到該數(shù)據(jù)
select * from users where u_name = '劉亦菲';#⑥mysql2 沒(méi)有查詢(xún)到該數(shù)據(jù)
commit;#⑦mysql1 提交之前的操作 此時(shí)mysql2在查詢(xún)發(fā)現(xiàn)之前查詢(xún)到的數(shù)據(jù)都變了 出現(xiàn)了不可重復(fù)讀
#首先設(shè)置mysql1和mysql2隔離級(jí)別為read commited
set session transaction isolation level read committed;
#禁用mysql1 mysql2的自動(dòng)提交
set autocommit = 0;
update users set u_name='李四' where id = 3;#① mysql1修改數(shù)據(jù)但未提交
select * from users where id =3;#② mysql1 查詢(xún)數(shù)據(jù) 為李四
select * from users where id =3;#③ mysql2 查詢(xún)數(shù)據(jù) 為張三0 沒(méi)有出現(xiàn)臟讀
insert into users (u_name) values ('劉亦菲');#④mysql1 插入數(shù)據(jù)但未提交
select * from users where u_name = '劉亦菲';#⑤mysql1 查詢(xún)到該數(shù)據(jù)
select * from users where u_name = '劉亦菲';#⑥mysql2 沒(méi)有查詢(xún)到該數(shù)據(jù)
commit;#⑦mysql1 提交之前的操作 此時(shí)mysql2在查詢(xún)發(fā)現(xiàn)之前查詢(xún)到的數(shù)據(jù)都變了 出現(xiàn)了不可重復(fù)讀
#首先設(shè)置mysql1和mysql2隔離級(jí)別為repeatable read
set session transaction isolation level repeatable read;
#禁用mysql1 mysql2的自動(dòng)提交
set autocommit = 0;
update users set u_name='李四' where id = 3;#① mysql1修改數(shù)據(jù)但未提交
select * from users where id =3;#② mysql1 查詢(xún)數(shù)據(jù) 為李四
select * from users where id =3;#③ mysql2 查詢(xún)數(shù)據(jù) 為張三0 沒(méi)有出現(xiàn)臟讀
insert into users (u_name) values ('劉亦菲');#④mysql1 插入數(shù)據(jù)但未提交
select * from users where u_name = '劉亦菲';#⑤mysql1 查詢(xún)到該數(shù)據(jù)
select * from users where u_name = '劉亦菲';#⑥mysql2 沒(méi)有查詢(xún)到該數(shù)據(jù)
commit;#⑦mysql1 提交之前的操作 此時(shí)mysql2還是沒(méi)有數(shù)據(jù)
在一個(gè)事務(wù)中可以設(shè)置保存點(diǎn),和rollback搭配使用
select * from users;
+------+-----------+
| id | u_name |
+------+-----------+
| 2 | 張三 |
| 3 | 李四 |
| 4 | 張三1 |
| 5 | 張三2 |
| 1006 | 劉亦菲 |
+------+-----------+
delete from users where id = 4;
savepoint a;#設(shè)置保存點(diǎn)
delete from users where id = 1006;
rollback to a;#回滾到保存點(diǎn)的位置
select * from users;
+------+-----------+
| id | u_name |
+------+-----------+
| 2 | 張三 |
| 3 | 李四 |
| 5 | 張三2 |
| 1006 | 劉亦菲 |
+------+-----------+
由于本身的博客百度沒(méi)有收錄,博客地址http://zhhll.icu
聯(lián)系客服