-
2024.08.23 Deadlock on rollback, but not on commit, when using ON DUPLICATE KEYTIL 2024. 8. 23. 22:37
MySQL :: MySQL 8.4 Reference Manual :: 17.7.3 Locks Set by Different SQL Statements in InnoDB
17.7.3 Locks Set by Different SQL Statements in InnoDB A locking read, an UPDATE, or a DELETE generally set record locks on every index record that is scanned in the processing of an SQL statement. It does not matter whether there are WHERE conditions in
dev.mysql.com
오늘은 insert on duplicate key update를 봤습니다.봤습니다. 기존 insert 문은 중복키 에러(동시 insert시) 발생 시 s lock을 획득합니다.
insert on duplicate key update문은 s lock대신 x lock을 획득하려 한다고 합니다.
다만 uk를 사용 시 next key lock도 얻으려해서 gap lock을 잡습니다.
이로 인해 동시 insert시 중복키 에러가 발생했을 때 동일하게 dead lock이 감지됩니다.음 실습해보는데 pk를 사용해도 rollback 시 gap이 잡히는 경우가 있다.
아래 질문과 같은 상황이다.
처음 duplicate key error로 대기할 때 insert intention lock을 서로 걸고 서로 lock을 획득하는 것인가?
그리고 rollback하니 x lock이 부여되는데 insert intention lock 때문에 서로 교착상태가 나는 것?
rollback 시 x lock이 동시에 부여될 수 있나? x lock은 안되지 않나?
gap lock의 locking read와 같은 상황으로 볼 수 있나?
명확한 답변이 없어서 더 살펴봐야겠다.
Inser in duplicate-key는 중복키 에러 시 x lock을 얻는다고 했습니다.
실습을 해보니 x lock을 얻으려고하지만 먼저 선점한 세션이 롤백하니 나머지 트랜잭션에서 dead lock이 발생합니다.
의심되는 것은 insert intention lock을 x lock 기다리기전에 획득했고 선점 트랜잭션이 rollback되면서 x lock을 획득하려다 서로의 insert intention lock때문에 충돌입니다.MySQL 5.7 - Deadlock on rollback, but not on commit, when using ON DUPLICATE KEY
Background: I have an application that has many concurrent threads. In one spot, I want to update a certain database row, but I cannot be sure that the row actually exists yet. Hence, I need to eit...
stackoverflow.com
'TIL' 카테고리의 다른 글
2024.09.02 data replicated (0) 2024.09.03 2024.08.28 system design interviews (0) 2024.08.28 2024.08.20 foreign key (0) 2024.08.20 2024.08.19 foreign key (0) 2024.08.19 2024.08.16 (0) 2024.08.16