직접 @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 객체 상태
갱신 후 트랜잭션 단위 메소드 종료 전, 객체는 바뀌었지만 db update는 아직 실행되지 않았다.
트랜잭션 메소드 종료 후, update쿼리가 실행되었다.
@Transactional 자체가 해당 메소드 로직 실행 전후에 트랜잭션 시작/종료를 시킬 수 있도록 프록시를 구성한다고 알고 있으므로 위 사실은 그를 간단하게 확인해볼 수 있는 테스트다.
'Spring' 카테고리의 다른 글
[Spring boot] 같은 url, 다른 parameter (0) | 2022.05.16 |
---|