-
Notifications
You must be signed in to change notification settings - Fork 0
@Async + @Transactional 사용 시 데이터 불일치 문제 해결 #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
PR Reviewer Guide 🔍(Review updated until commit dbbe6c5)Here are some key observations to aid the review process:
|
|
Persistent review updated to latest commit dbbe6c5 |
User description
관련 이슈 (Related Issues)
🚀 PR 개요
Answer–Feedback 비동기 처리 구조를 리팩토링하여 데이터 정합성을 보장하고 성능을 유지했습니다.
📌 변경 배경
@Async@Transactional이 적용된 FeedbackService.saveFeedback()을 직접 호출하는 방식이었습니다.🔍 원인 분석
Spring의
@Async는 별도의 스레드와 별도의 트랜잭션에서 실행됩니다. 따라서 AnswerService.saveAnswer()에서 예외 발생 시 Answer와 Question은 롤백되지만, Feedback은 이미 커밋되어 데이터 불일치가 발생할 수 있었습니다.✅ 해결 과정
@TransactionalEventListener(phase = AFTER_COMMIT)에서만 Feedback 생성이 실행되도록 보장@Async로 비동기 처리하여 서비스 응답 속도 유지🎯 결과
PR Type
Bug fix, Tests, Enhancement
Description
이벤트 기반으로 피드백 생성 전환
트랜잭션 커밋 후 비동기 처리 보장
콘솔 로그 제거 및 예외 처리 추가
단위 테스트 추가 및 검증 강화
Diagram Walkthrough
File Walkthrough
AnswerSavedEvent.java
Answer 저장 이벤트 객체 도입src/main/java/com/project/InsightPrep/domain/question/event/AnswerSavedEvent.java
AnswerSavedEvent레코드 추가FeedbackEventListener.java
트랜잭션 후 비동기 피드백 생성 리스너src/main/java/com/project/InsightPrep/domain/question/event/FeedbackEventListener.java
AnswerServiceImpl.java
서비스에서 이벤트 발행으로 피드백 분리src/main/java/com/project/InsightPrep/domain/question/service/impl/AnswerServiceImpl.java
ApplicationEventPublisher주입AnswerSavedEvent발행FeedbackServiceImpl.java
디버그 출력 제거로 정리src/main/java/com/project/InsightPrep/domain/question/service/impl/FeedbackServiceImpl.java
FeedbackEventListenerTest.java
이벤트 리스너 단위 테스트 추가src/test/java/com/project/InsightPrep/domain/question/event/FeedbackEventListenerTest.java
AnswerServiceImplTest.java
Answer 서비스 테스트를 이벤트 기반으로 수정src/test/java/com/project/InsightPrep/domain/question/service/impl/AnswerServiceImplTest.java
AnswerSavedEvent캡처 및 값 검증