Quartz

Quartz Debugging

QuartzJobBean BeanInjection

  • 서비스 빈 주입하고 싶었지만 null
  • the preferred way to apply dependency injection to Job instances is via a JobFactory: that is, to specify SpringBeanJobFactory as Quartz JobFactory (typically via SchedulerFactoryBean's "jobFactory" property). This allows to implement dependency-injected Quartz Jobs without a dependency on Spring base classes.
 
💡
프레임워크나 라이브러리도 용빼는 재주가 있는건 아니기 때문에 디버거로 소스 코드를 확인하면서 오류를 해결한다.

LazyInitializationException

failed to lazily initialize a collection of role: com.letmeknow.domain.BoardNumber.articles, could not initialize proxy - no Session

배경

  • Job 클래스에서 JPA를 통해 데이터를 가져오고, Entity 클래스를 수정, 접근하고 싶었다.

원인

  • 영속성 컨텍스트의 생존 범위때문이었다.
  • Spring Boot에서의 트랜잭션 범위 기본값은 Repository, Service다.
  • Job 클래스는 영속성 컨텍스트 생존 범위 밖이기 때문에 세션이 없어 프록시를 초기화할 수 없는 것이다.
notion image

해결

  • Service가 DTO를 반환하도록 하고, Job 클래스에서 처리한 후, DTO로 다시 변환하여 Service 로직 내에서 데이터를 저장한다.