Skip to content

Conversation

@JunbeomKoreaUniv
Copy link
Contributor

@JunbeomKoreaUniv JunbeomKoreaUniv commented Mar 9, 2025

수정한 코드

  • (핵심 변경 사항)알림 스케줄러에 @Transactional(readOnly = True)를 걸어주었음.
  • (기타 변경 사항) findSchedulesStartingAt의 JPQL을 변경하였음.
    -(변경 전) FUNCTION 사용하여 약속을 가져와 DB에 종속적임.
    -(변경 후) BETWEEN을 사용하여 약속을 가져와 DB에 독립적으로 실행될 수 있음.

발생했던 문제

Schedule에서 getUser로 가져온 User(프록시 객체)의 firebaseToken을 사용(getFirebaseToken)하려는 순간 지연로딩 에러가 발생함.

문제 원인

@Scheduled 애너테이션이 별도의 스레드에서 실행되고 트랜잭션이 없어 영속성 컨텍스트가 없어 Schedule에서 User를 지연로딩할 수 없었음.(프록시 객체로만 존재)

문제 해결

알림 스케줄러 전체에 @Transactional(readOnly = True)를 걸어 @Scheduled가 포함된 메서드들이 트랜잭션이 적용되게 하여 영속성 컨텍스트가 생성되게 해 문제 해결하였음.

mac added 4 commits March 7, 2025 09:18
- Schedule에서 getUser로 가져온 User(프록시 객체)의 firebaseToken을 사용(getFirebaseToken)하려는 순간 지연로딩 에러가 발생함.
- @scheduled 애너테이션이 별도의 스레드에서 실행돼 영속성 컨텍스트가 기존과 달라 발생한 문제임.
- @transactional을 걸어 기존 영속성 컨텍스트가 전파되게 하여 문제 해결하였음.
System.out.println("5분 후 시간: " + baseTime);

// 5분 후의 scheduleTime과 일치하는 약속 조회
List<Schedule> schedulesStartingSoon = scheduleRepository.findSchedulesBetween(startTime, endTime);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findSchedulesStartingAt 사용하는 대신에 findSchedulesBetween 하나의 메서드로 사용을 통일하셨군요.
BETWEEN을 사용하면 인덱스를 사용할 확률이 높아 성능이 좋아지니 이 방법을 잘 선택하신 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

성능쪽은 고려하지 않은 부분인데 오..! 원래 LocalDateTime 데이터를 그대로 사용하니 조회 시간이 더 줄어들 여지가 있겠네요. 감사합니다!

import java.util.List;

@Component
@Transactional(readOnly = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@transactional(readOnly = true)를 추가하여 트랜잭션을 활성화함으로써 Lazy Loading 문제를 잘 해결하셨네요!

@jjjh02
Copy link
Contributor

jjjh02 commented Mar 9, 2025

수고하셨습니다 ~

@jjjh02 jjjh02 merged commit 4cac536 into main Mar 9, 2025
2 checks passed
@jjjh02 jjjh02 deleted the hotfix/notification-scheduler branch March 9, 2025 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants