事务间锁冲突与共存,√为共存,×为互斥
| 共享锁(S) | 排他锁(X) | |
|---|---|---|
| 共享锁(S) | √ | × | 
| 排他锁(X) | × | × | 
共享锁:又称读锁,多个事务可以共享一把读锁,只能读但不能修改;
排他锁:又称写锁,不能与其它锁共存,包括共享锁。
几个知识点:
- 共享锁,通过lock in share mode加锁
 例:select * from t_test where id=1 lock in share mode;for update加锁
 例:select * from t_test where id=1 for update;
- 上面两种锁都得在事务间才生效,直接执行会马上释放锁。begin, xxx, commit
- 一个事务加上S锁,其它事务只能读,不能写;
 一个事务加上X锁,其它事务不能写,且不能加读锁;

Comments