赞
踩
-- 开启事务
START TRANSACTION | BEGIN
-- 提交事务,并使得已对数据库做的所有修改持久化
COMMIT
-- 回滚事务,结束用户的事务,并撤销正在进行的所有未提交的修改
ROLLBACK
-- 创建一个保存点,一个事务可以有多个保存点
SAVEPOINT identifier
-- 删除一个保存点
RELEASE SAVEPOINT identifier
-- 事务回滚到保存点
ROLLBACK TO [SAVEPOINT] identifier
-- 设置隔离级别 SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL REPEATABLE READ; -- 或者采用下面的方式设置隔离级别 SET @@tx_isolation = 'REPEATABLE READ'; SET @@global.tx_isolation = 'REPEATABLE READ'; -- 查看全局隔离级别 SELECT @@global.tx_isolation; -- 查看当前会话隔离级别 SELECT @@session.tx_isolation; SELECT @@tx_isolation; -- 手动给读操作加 S 锁 SELECT ... LOCK IN SHARE MODE; -- 手动给读操作加 X 锁 SELECT ... FOR UPDATE; -- 查看当前锁信息 SELECT * FROM information_schema.innodb_locks;
seq | session A | session B |
---|---|---|
1 | SET @@tx_isolation=‘READ UNCOMMITTED’; | SET @@tx_isolation=‘READ UNCOMMITTED’; |
2 | BEGIN; | |
3 | UPDATE account_t SET money = money - 100 WHERE name = ‘A’; | |
4 | BEGIN; | |
5 | SELECT money FROM account_t WHERE name = ‘A’; | |
6 | COMMIT | COMMIT |
seq | session A | session B |
---|---|---|
1 | SET @@tx_isolation=‘READ COMMITTED’; | SET @@tx_isolation=‘READ COMMITTED’; |
2 | BEGIN; | BEGIN; |
3 | SELECT money FROM account_t WHERE name = ‘A’; | |
4 | UPDATE account_t SET money = money - 100 WHERE name = ‘A’; | |
5 | COMMIT; | |
6 | SELECT money FROM account_t WHERE name = ‘A’; | |
6 | COMMIT; |
快照读和当前读不一致
。seq | session A | session B |
---|---|---|
1 | SET @@tx_isolation=‘REPEATABLE READ’; | SET @@tx_isolation=‘REPEATABLE READ’; |
2 | BEGIN; | BEGIN; |
3 | SELECT * FROM account_t WHERE id >= 2; | |
4 | INSERT INTO account_t(id,name,money) VALUES (4,‘D’,1000); | |
5 | COMMIT; | |
6 | INSERT INTO account_t(id,name,money) VALUES (4,‘D’,1000); # 报错,因为幻读 |
seq | session A | session B |
---|---|---|
1 | SET @@tx_isolation=‘REPEATABLE READ’; | SET @@tx_isolation=‘REPEATABLE READ’; |
2 | BEGIN; | BEGIN; |
3 | SELECT * FROM account_t WHERE id >= 2 lock in share mode; | |
4 | INSERT INTO account_t(id,name,money) VALUES (4,‘D’,1000); # 等待执行 | |
5 | INSERT INTO account_t(id,name,money) VALUES (4,‘D’,1000); | # 等待执行 |
6 | COMMIT; | # 报错,因为破坏了数据库完整性约束 |
select * from table where ?
select * from table where ? lock in share mode # S 锁(读锁)
select * from table where ? for update # X 锁(写锁)
insert into table values(...)
update table set ? where ?
delete from table where ?
flush tables with read lock # 整个数据库处于只读状态
unlock tables
lock tables 'table' [read/write]
unlock tables
crud
alter
锁 | GAP(持有) | Insert Intention(持有) | Record(持有) | Next-key(持有) |
---|---|---|---|---|
GAP(请求) | 兼容 | 兼容 | 兼容 | 兼容 |
Insert Intention(请求) | 冲突 | 兼容 | 兼容 | 冲突 |
Record(请求) | 兼容 | 兼容 | 冲突 | 冲突 |
Next-key(请求) | 兼容 | 兼容 | 冲突 | 冲突 |
UPDATE students SET score = 100 WHERE id = 15;
UPDATE students SET score = 100 WHERE id = 16;
UPDATE students SET score = 100 WHERE no = 'S0003';
UPDATE students SET score = 100 WHERE no = 'S0008';
UPDATE students SET score = 100 WHERE name = 'Tom';
UPDATE students SET score = 100 WHERE name = 'John';
UPDATE students SET score = 100 WHERE score = 22;
UPDATE students SET score = 100 WHERE id <= 20;
UPDATE students SET score = 100 WHERE age <= 23;
UPDATE students SET name = 'John' WHERE id = 15;
-- 开启标准监控
CREATE TABLE innodb_monitor (a INT)
ENGINE=INNODB;
-- 关闭标准监控
DROP TABLE innodb_monitor;
-- 开启锁监控
CREATE TABLE innodb_lock_monitor (a INT)
ENGINE=INNODB;
-- 关闭锁监控
DROP TABLE innodb_lock_monitor
-- 开启标准监控
set GLOBAL innodb_status_output=ON;
-- 关闭标准监控
set GLOBAL innodb_status_output=OFF;
-- 开启锁监控
set GLOBAL innodb_status_output_locks=ON;
-- 关闭锁监控
set GLOBAL innodb_status_output_locks=OFF;
-- 将死锁信息记录在错误日志中
set GLOBAL innodb_print_all_deadlocks=ON;
-- 查看事务
select * from information_schema.INNODB_TRX;
-- 查看锁
select * from information_schema.INNODB_LOCKS;
-- 查看锁等待
select * from information_schema.INNODB_LOCK_WAITS;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。