Isolation Annotation
Test
@Test
void isolationTest(){
Book book = new Book();
book.setName("JPA강의");
bookRepository.save(book);
bookService.get(1L); //DB를 지우고 바로 테스트를 하기 때문에 book의 정보가 1번
System.out.println(">>> " + bookRepository.findAll());
}
BookService.java
@Transactional(isolation = Isolation.READ_UNCOMMITTED) //지정
public void get(Long id){
System.out.println(">>> "+bookRepository.findById(id));
System.out.println(">>> "+bookRepository.findByAll());
System.out.println(">>> "+bookRepository.findByAId(id));
System.out.println(">>> "+bookRepository.findByAll());
}
sql
mysql> start transaction;
Query Ok, 0 rows affacted (0.00sec)
mysql> update book set category='none';
Query Ok, 0 rows affacted (0.00sec)
Rows matched: 1 Changed: 1 Warnings:0
result
-> debug로 get()의 두번째 컬럼까지 실행결과 (Dirty Read)
중간에 업데이트 구문을 하나 추가해서 실행
sql
mysql> rollback;
Query Ok, 0 rows affacted (0.00sec)
BookService.java
@Transactional(isolation = Isolation.READ_UNCOMMITTED) //지정
public void get(Long id){
System.out.println(">>> "+bookRepository.findById(id));
System.out.println(">>> "+bookRepository.findByAll());
System.out.println(">>> "+bookRepository.findByAId(id));
System.out.println(">>> "+bookRepository.findByAll());
Book book = bookRepository.findById(id).get();
book.setName("바뀔까?");
bookRepository.save(book);
}
sql
mysql> start transaction;
Query Ok, 0 rows affacted (0.00sec)
mysql> update book set category='none';
Query Ok, 0 rows affacted (0.00sec)
Rows matched: 1 Changed: 1 Warnings:0
result
Dirty Read 후 Update쿼리가 생성되었음
코드가 종료 되지 않고 대기상태로 전환
sql
mysql> commit;
Query Ok, 0 rows affacted (0.00sec)
다음 브레이크 포인트로 이동됨, 디버깅 계속 진행
다음과 같이 name이 바뀔까?로 변경된걸 확인할 수 있다.
#패스트캠퍼스 #패캠챌린지 #직장인인강 #직장인자기계발 #패스트캠퍼스후기 #한번에끝내는Java/Spring웹개발마스터초격차패키지Online
본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성되었습니다.