• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

@Scheduled 사용 시 @Transactional 메소드 사용 실패 관련

21.08.30 18:29 작성 조회수 2.29k

1

안녕하세요, 강의 잘 듣고 있습니다!!

JPA 사용 중에 강의만으로는 어려운 문제를 만나 문의드립니다! JPA 트랜잭션에 대한 오류인것 같고 꽤 오래 찾아았으나,  아직 답을 찾지 못했습니다.

현상은 @Scheduled 가 추가된 메소드에서 다른 서비스 A의 @Transactional 메소드를 호출 시 아래와 같은 에러가 발생합니다. 

예시) 

@Transactional

public void changeStatus(Long id, String status) {

  Obj o = objRepository.findById(id).get();

   o.setStatus(status);

}

환경

SpringBoot 2.4 이며, 자동으로 설정되는  JpaTransactionManager 를 사용합니다. 

==============

에러 메시지

==============

2021-08-30 18:14:17.536 ERROR 18252 --- [ scheduler-1] o.s.s.s.TaskUtils$LoggingErrorHandler.handleError:95 - Unexpected error occurred in scheduled task org.springframework.orm.jpa.JpaSystemException: Unable to perform beforeTransactionCompletion callback: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.; nested exception is org.hibernate.HibernateException: Unable to perform beforeTransactionCompletion callback: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:331)

Trace 로그로 볼 때에는 transaction marked for rollback only 라는 메시지도 나옵니다. 

변경 감지로 할 경우와, save 메소드를 호출하는 경우 모두 발생하구요.. 

제가 무언가 놓치고 있는 게 있다면, 조언 부탁드립니다!!

답변 3

·

답변을 작성해보세요.

1

도움을 드리고 싶은데, 코드를 보아야 할 것 같습니다.

해당 예시만 확인할 수 있는 프로젝트를 새로 만들어주세요. 다른 코드가 섞여 있으면 분석이 어려우니 최대한 프로젝트를 문제부분만 나타내도록 단순하게 새로 만들어주세요

전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

주의: 업로드시 권한 문제 꼭 확인해주세요

추가로 다음 내용도 코멘트 부탁드립니다.

1. 실행 방법을 알려주세요.

2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.

감사합니다.

0

제가 본문에 다른 서비스 A라고만 한게 설명이 좀 부족했었나봅니다 ^^;

안그래도 아래와 같은 글을 보고 @Schedule 클래스와 @Transacional이 있는 xxService를 분리하여 호출을 시도했음에도 결과는 동일하더라고요.. 

https://stackoverflow.com/a/18401709

짐작해볼만한 다른 이유가 또 있을까요..??

항상  감사드립니다~!

0

안녕하세요. 인프런21님

이렇게 풀어보시겠어요?

중요한 것은 @Schedule 클래스와 @Transacional이 있는 xxService를 분리해야 합니다.

class schedule {

  @Schedule

  go() {

    service.call();

  }

}

@Transactional

class xxService {

  call() {...};

}

감사합니다.