Study
shared lock, exclusive lock
voider
2022. 8. 15. 00:27
InnoDB implements standard row-level locking where there are two types of locks, shared (S) locks and exclusive (X) locks.
- A shared (S) lock permits the transaction that holds the lock to read a row.
- An exclusive (X) lock permits the transaction that holds the lock to update or delete a row.
If transaction T1 holds a shared (S) lock on row r, then requests from some distinct transaction T2 for a lock on row r are handled as follows:
- A request by T2 for an S lock can be granted immediately. As a result, both T1 and T2 hold an S lock on r.
- A request by T2 for an X lock cannot be granted immediately.
If a transaction T1 holds an exclusive (X) lock on row r, a request from some distinct transaction T2 for a lock of either type on r cannot be granted immediately. Instead, transaction T2 has to wait for transaction T1 to release its lock on row r.
InnoDB
는 shared lock
과 exclusive lock
두 가지 유형의 잠금을 구현한다.
shared lock
은 Lock을 가지고 있는 트랜잭션이 행을 읽을 수 있다.exclusive lock
은 Lock을 가지고 있는 트랜잭션이 행을 수정하거나 삭제할 수 있다.
만약 트랜잭션 T1
이 r
이라는 행의 shared lock을 가지고 있는데, 또 다른 트랜잭션 T2
가 행 r
을 요청하면 다음처럼 처리된다.
- shared lock에 대한
T2
의 요청은 즉시 승인된다. 결과적으로,T1
과T2
모두r
행에 대해 shared lock을 가진다. T2
가 exclusive lock에 대해 요청했다면 즉시 승인되지 않는다.
만약 트랜잭션 T1
이 r
행의 exclusive lock을 가지고 있다면, 트랜잭션 T2
의 r
에 대한 요청은 즉시 승인 되지 않는다. T2
는 T1
이 exclusive lock을 풀 때까지 기다려야 한다.