#Mysql中的锁:
Myisam和innodb都支持表锁,innodb支持行锁。
##表锁:
1、读锁
锁住之后,本会话和其他会话都可以读取,都不能够写入。 只有解锁之后才能写入。
lock table users read;
unlock tables;
2、写锁
锁住之后,本会话可以读取和写入,其他会话不能读取写不能写入。
lock table users write;
unclock tables;
##行锁:
行锁是索引级别的,不是记录级别的,如果where字段没有加索引(包括读取的时候和更新的时候),则锁表。
共享锁:
共享锁锁住的行,本回话可以写入和读取,其他会话只能读取,不能写入。其他会话只有事务提交之后才能写入。
start transaction;
select * from users where id=1 lock in share mode; #使用共享锁锁住id=1这一行。
commit;
排他锁:
排他锁锁住的行,本回话可以读取和写入,其他会话只能读取(只能普通的读:不能使用锁),不能写入。其他会话只有事务提交之后才能写入。
start transaction;
select * from users where id=1 for update; #使用排他锁锁住id=1这一行。
commit;