@Trasncational을 통한 선언적 트랜잭션 관리 방식에서 Self Invocation 문제가 발생하는 이유
자기 호출(Self Invocation)
@Service
@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;
private final UserService userService;
private final EntityManager entityManager;
public void externalCreatePost(PostCreateRequest postCreateRequest) {
internalCreatePost(postCreateRequest);
}
@Transactional
public void internalCreatePost(PostCreateRequest postCreateRequest) {
User user = userService.getUserById(postCreateRequest.getUserId());
postRepository.save(postCreateRequest.toEntity(user));
System.out.println(entityManager.isJoinedToTransaction()); // false
if (true) {
throw new RuntimeException();
}
}
}Self Invocation 해결 방법
예방 방법

REQUIRES_NEW != 독립적인 메서드
externalCreatePost
internalCreatePost
결론
Last updated