====== Isolation and Lock ====== Transaction의 Isolation Level(隔離レベル)과 Lock에 대해서 살펴본다. ===== Isolation ===== Isolation level에 대해서 얘기할 때 다음의 3가지를 고려해야 한다. 기차표 좌석의 예매, 조회 등 동시에 Transaction이 발생할 경우를 예를 들어 설명한다. ^ 구분 ^ 특징 ^ 설명 ^ |dirty reads \\ (read uncommitted)|① Time 10:00:00 : tranaction1이 cabin99좌석을 예매 \\ ② Time 10:00:02 : transaction2가 좌석수를 조회. 예매 가능좌석이 2자리 \\ ③ Time 10:00:03 : transaction1이 좌석예매를 취소 \\ ④ Time 10:00:04 : transaction2가 종료후에는 이미 잘못된 데이타를 갖고있게 됨| | |repeatable reads \\ (read consistency)|① Time 10:00:00 : transaction1이 명시적으로 transaction.begin() \\ ② Time 10:00:01 : transactin1이 좌석수 조회. 결과 잔여석 2 \\ ③ Time 10:00:02 : transaction2가 좌석수를 2에서 으로 변경 \\ ④ Time 10:00:03 : transactin1이 좌석수 조회. 결과 잔여석 2 |같은 transaction내에서 읽기 일관성이 유지되는 것을 말한다 | |phantom reads|① Time 10:00:00 : transaction1이 명시적으로 transaction.begin() \\ ② Time 10:00:01 : transactin1이 좌석수 조회. 결과 잔여석 2. cabin99 좌석예매가능\\ ③ Time 10:00:02 : transaction2가 cabin99좌석 예매. Reservation table에 추가 \\ ④ Time 10:00:03 : transactin1이 좌석수 조회. 결과 잔여석 2. 하지만 cabin99 예매 불가|transaction1의 첫번째 조회에는 보이지 않았지만, 두번째 조회에는 보였으므로 Reservation table에 추가된 cabin99예매 데이타는 phantom data | ===== Database Locks ===== Database는 여러가지 Locking mechanism을 사용하는데, 흔히 다음의 read lock, write lock, exclusive write lock, snapshot을 말한다. ^구분^특징^설명^ |read locks|읽기가 발생할 경우 다른 transaction의 쓰기를 금지한다. \\ 그러므로 nonrepetable reads를 방지한다. \\ 다른 transaction은 읽기는 가능하지만 역시 쓰기는 금지당한다. \\ 현재 읽고 있는 transaction 역시 쓰기 금지한다.| | |write locks|현재 traction의 update가 종료될때까지 다른 tranaction의 쓰기를 금지한다. \\ write lock은 현재 transaction을 포함해 다른 transaction의 dirty reads를 허용한다. \\ 즉 현재 transaction의 commit되지 않은 data가 반영된다.| | |exclusive write locks|현재 transaction의 쓰기가 종료될때까지 다른 transaction의 읽기,쓰기를 금지한다. \\ 그러므로 다른 transaction에 의한 dirty reads를 방지할 수 있다. \\ 어떤 vendor의 database는 자신의 data 읽기도 금지한다.| | |snapshots|transaction이 시작될때 저장된 frozen view이다. \\ snapshots은 dirty reads, nonrepeatable reads, phantom reads를 방지할 수 있지만, data가 real-time이 아니므로 주의해야 한다.| | ===== Transaction Isolation Levels ===== ^isolation level^dirty reads^nonrepeatable reads^phantom reads^%%ControlDescriptor Constant%%^ |Read Uncommitted| 〇 | 〇 | 〇 | TRANSACTION_READ_UNCOMMITTED | |Read Committed| × | 〇 | 〇 | TRANSACTION_READ_COMMITTED | |Repeatable Read| × | × | 〇 | TRANSACTION_REPEATABLE_READ | |Serializable| × | × | × | TRANSACTION_SERIALIZABLE | ===== reference ===== - [[http://www.unix.com.ua/orelly/java-ent/ebeans/ch08_03.htm]] : Isolation and Database Locking