2024.05.22 - [개발/sql] - DB isolation level
DB isolation level
isolation level 이란 무엇인가?디비 동시성을 관리하고 데이터 정합성을 유지하기 위해 서로 다른 트랜젝션끼리의 관계를 정의한 것 존재 이유?언제 그리고 어떤 식으로 한 트랜젝션이 만든 변화
bangpurin.tistory.com
Isolation Level은 동시 트랜잭션이 수행될때 다른 트랜잭션이 동일한 데이터에 대해서 어떻게 보일지에 대한 범위를 나타낸다.
Isolation is the third letter in the ACID acronym. It means that concurrent transactions should not impact each other.
위에서 DB단에서의 isolation level에 대해 살펴보았다. 이제 application 단에서의 isolation 설정을 알아보자.
isolation level을 아래와 같이 트랙잭션 별로 설정할 수 있다.
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void readUncommittedTransaction() {
// Your code here
}
@Transactional(isolation = Isolation.READ_COMMITTED)
public void readCommittedTransaction() {
// Your code here
}
@Transactional(isolation = Isolation.REPEATABLE_READ)
public void repeatableReadTransaction() {
// Your code here
}
@Transactional(isolation = Isolation.SERIALIZABLE)
public void serializableTransaction() {
// Your code here
}
- Isolation.DEFAULT: 디비의 default 설정에 따름
- Isolation.READ_UNCOMMITTED
- Isolation.READ_COMMITTED: 처음 업데이트 값으로 오버라이드 위험
- Isolation.REPEATABLE_READ: 오버라이드 할 것 같으면 에러 발생
- 에러 발생하면 재시도할 수 있음
- @Retryable(maxAttempts = 15) // This is actually quite a lot. Ideally, 1–3 attempts should be sufficient.
- 스래드 200개 생성됨..(과다하게 생성됨; 동시성 이슈)
- 즉각 재시도(디비저장) 보다 다시 큐를 쌓도록하는게 동시성을 낮출 수
- Isolation.SERIALIZABLE
- 동시에 저장하려고하면 디비에서 에러발생
- 그래도 100프로 보장 못함; 실패나면 롤백되는게 있음
주의사항
데이터 정합성(data integrity)과 성능(performance)을 고려하여 설정해야 한다.
사용하고자 하는 level 이 DB에서 지원하는 레벨인지 확인해야 한다.
애플리케이션 전체 기본 설정을 바꾸려면 아래와 같이 설정값을 추가한다.
(spring boot version 확인하고 넣도록 하자)
springboot 2.7.3 ~ 3.2에는 아래와 같다.
spring.datasource.hikari.transaction-isolation
https://docs.spring.io/spring-boot/docs/2.7.3/reference/htmlsingle/
Spring Boot Reference Documentation
This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe
docs.spring.io
Common Application Properties
docs.spring.io
'개발 > spring' 카테고리의 다른 글
[retry] spring-retry 다시 복기 (0) | 2024.05.24 |
---|---|
[jpa] transaction propagation (0) | 2024.05.23 |
[interceptor] endpoint 가 없는데도 404가 아니라 200이 떨어져요! (1) | 2024.04.18 |
[application.yml] 프로파일 옵션으로 배포 설정 분리 (0) | 2024.02.29 |
[이슈해결][jpa] native query에서 사용자 변수 사용 시 (0) | 2024.02.07 |