뭐든지시작이반이다
Dream Future
뭐든지시작이반이다
  • 분류 전체보기
    • Spring
      • 개념
      • 기타
    • Java
    • Algorithm
      • 알고리즘 정리
    • DB
      • Postgresql
    • 트러블슈팅
    • Git & Github
    • V&V
    • EFK
    • 북스터디
    • 기타

인기 글

최근 댓글

전체 방문자
오늘
어제

블로그 메뉴

  • 홈
  • 태그
  • 방명록

태그

  • mybatis
  • springboot
  • JPA
  • auditing
  • spring-boot
  • Kibana
  • sqlSessionTemplate
  • github
  • git
  • Kotlin
  • lombok
  • springbot
  • gradle
  • requestmapping
  • gitignore
  • ambiguous오류
  • docker
  • 파티셔닝
  • efk
  • Hibernate
  • SelectionSort
  • 트러블슈팅
  • 알고리즘
  • 대규모서비스
  • fluentd
  • multimodule
  • spring
  • dirtyChecking
  • Controller
  • SqlSessionFactory
hELLO · Designed By 정상우.
뭐든지시작이반이다

Dream Future

[Spring boot] JPA dirty checking 시행 시점
Spring

[Spring boot] JPA dirty checking 시행 시점

2022. 6. 16. 17:37

직접 @Transactional 어노테이션이 달린 메소드를 실행해보며 언제 db에 저장되는지 확인해보자

 

PostApiController.java

    @PostMapping("api/v1/post/{id}")
    public PostResponseDto update(@PathVariable Long id, @RequestBody PostUpdateRequestDto requestDto){
        System.out.println("transaction 시작");
        PostResponseDto dto= postService.update(id, requestDto);
        System.out.println("transaction 종료");
        return dto;
    }

PostService.java

    @Transactional
    public PostResponseDto update(Long id, PostUpdateRequestDto requestDto) {
        Post post=postRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("해당하는 게시글이 없습니다. id: "+id));

        post.update(requestDto.getTitle(), requestDto.getContent());

        return PostResponseDto.builder().post(post).build();
    }

서비스 update메소드의 update, return, 컨트롤러 트랜잭션 종료 출력 부분에 break를 걸고 디버그 모드로 실행하여 확인해본다.

먼저, 업데이트 전 post의 상태는 다음과 같았다.

업데이트 전

post 객체 필드 갱신 전 entity 객체 상태

post update 실행 전 entity 객체 상태

갱신 후 트랜잭션 단위 메소드 종료 전, 객체는 바뀌었지만 db update는 아직 실행되지 않았다.

트랜잭션 단위 메소드 종료 전 객체
트랜잭션 단위 메소드 종료 전 로그 상태

트랜잭션 메소드 종료 후, update쿼리가 실행되었다.

update 쿼리 실행
db 반영

 

@Transactional 자체가 해당 메소드 로직 실행 전후에 트랜잭션 시작/종료를 시킬 수 있도록 프록시를 구성한다고 알고 있으므로 위 사실은 그를 간단하게 확인해볼 수 있는 테스트다.

 

저작자표시 비영리 동일조건 (새창열림)

'Spring' 카테고리의 다른 글

[Spring boot] 같은 url, 다른 parameter  (0) 2022.05.16
    'Spring' 카테고리의 다른 글
    • [Spring boot] 같은 url, 다른 parameter
    뭐든지시작이반이다
    뭐든지시작이반이다
    기록장입니다.

    티스토리툴바