*************************************************************************************************************** 实验一:观察INNODB_TRX、INNODB_LOCKS、InnoDB_LOCK_WAITS、processlist,status ************************************************************************************************************** create table gyj_t1(id int primairy key,name varchar(10)); insert into gyj_t1 values(1,‘AAAAA‘); mysql> show variables like ‘%autocommit%‘; mysql> select @@tx_isolation; mysql> show variables like ‘innodb_lock_wait_timeout‘; mysql> set global innodb_lock_wait_timeout=600; mysql> set innodb_lock_wait_timeout=600;
session 1 mysql> begin; mysql> update gyj_t1 set name=‘BBBBB‘ where id=1;
session 2 mysql> begin; mysql> update gyj_t1 set name=‘bbbbb‘ where id=1;
session 3 mysql> select * from information_schema.innodb_trx\G; mysql> select * from information_schema.innodb_locks\G; mysql> select * from information_schema.innodb_lock_waits\G; mysql> show processlist; mysql> show engine innodb status\G;
********************************************* 实验二:锁案例一,聚集索引上的锁 ********************************************** 1.默认RR隔离级别 2.自动提交 3.创建表 CREATE TABLE student ( id int unsigned not null auto_increment, xh int unsigned not null, name varchar(10) not null, bjmc varchar(20) not null, primary key(id), key xh(xh) ) engine =InnoDB;
5.场景二 set autocommit=0; (1)session 1 select * from student where name=‘guoyj‘ for update;
(2)session 2 select * from student where name=‘jfedu‘ for update; #这时侯会阻塞吗?(会)
(3)session 1 commit;或 rollback;
总结:看表结构,name这列没有索引,在RR隔离级别所有的记录全部都会被锁定,排它锁。
6.场景三 set autocommit=0; (1)session 1 select * from student where xh=1 and name=‘guoyj‘ for update; (2)session 2 select * from student where xh=1 and name=‘jfedu‘ for update; #这时侯会阻塞吗?(会) (3)session 1 commit;或 rollback;