-
Notifications
You must be signed in to change notification settings - Fork 3
refactor : 트랜잭션 범위 내 외부 서비스 호출 처리 #102
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,16 +12,20 @@ | |
| import org.ezcode.codetest.domain.user.service.UserDomainService; | ||
| import org.ezcode.codetest.infrastructure.s3.S3Directory; | ||
| import org.ezcode.codetest.infrastructure.s3.S3Uploader; | ||
| import org.ezcode.codetest.infrastructure.s3.exception.S3Exception; | ||
| import org.ezcode.codetest.infrastructure.s3.exception.code.S3ExceptionCode; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class ProblemService { | ||
|
|
||
| private final ProblemDomainService problemDomainService; | ||
|
|
@@ -39,8 +43,8 @@ public ProblemDetailResponse createProblem(ProblemCreateRequest requestDto, Mult | |
|
|
||
| // 문제 이미지 있다면? | ||
| if (image != null && !image.isEmpty()) { | ||
| String imageUrl = s3Uploader.upload(image, S3Directory.PROBLEM.getDir()); | ||
| savedProblem.addImage(imageUrl); | ||
| String imageUrl = uploadImageAfterTransaction(image, savedProblem.getId()); | ||
| updateProblemWithImage(savedProblem.getId(), imageUrl); | ||
| } | ||
|
|
||
| return ProblemDetailResponse.from(savedProblem); | ||
|
|
@@ -91,5 +95,20 @@ public void removeProblem(Long problemId) { | |
|
|
||
| problemDomainService.removeProblem(findProblem); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void updateProblemWithImage(Long problemId, String imageUrl) { | ||
| Problem problem = problemDomainService.getProblem(problemId); | ||
| problem.addImage(imageUrl); | ||
| } | ||
|
Comment on lines
+99
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 트랜잭션 전파 설정이 필요합니다. 현재 메서드는 기존 트랜잭션에 참여하게 되어 별도의 트랜잭션으로 동작하지 않습니다. 진정한 트랜잭션 분리를 위해서는 전파 설정이 필요합니다. 다음과 같이 트랜잭션 전파 설정을 추가하는 것을 권장합니다: -@Transactional
+@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateProblemWithImage(Long problemId, String imageUrl) {
Problem problem = problemDomainService.getProblem(problemId);
problem.addImage(imageUrl);
}🤖 Prompt for AI Agents |
||
|
|
||
| private String uploadImageAfterTransaction(MultipartFile image, Long problemId) { | ||
| try { | ||
| return s3Uploader.upload(image, S3Directory.PROBLEM.getDir()); | ||
| } catch (Exception e) { | ||
| log.error("Problem {} 이미지 업로드 실패", problemId, e); | ||
| throw new S3Exception(S3ExceptionCode.S3_UPLOAD_FAILED); | ||
| } | ||
| } | ||
|
Comment on lines
+105
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion S3 업로드 메서드가 여전히 트랜잭션 내에서 호출됩니다. 메서드명이 진정한 트랜잭션 분리를 위해 다음 중 하나를 고려해보세요:
+@Async
+@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
+public void handleImageUpload(ProblemImageUploadEvent event) {
+ try {
+ String imageUrl = s3Uploader.upload(event.getImage(), S3Directory.PROBLEM.getDir());
+ updateProblemWithImage(event.getProblemId(), imageUrl);
+ } catch (Exception e) {
+ log.error("Problem {} 이미지 업로드 실패", event.getProblemId(), e);
+ // 업로드 실패 시 별도 처리 로직 (예: 재시도 큐에 추가)
+ }
+}
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
트랜잭션 범위 분리가 불완전합니다.
현재 구현에서는 여전히 S3 업로드가 데이터베이스 트랜잭션 내에서 호출되고 있어 PR 목표가 완전히 달성되지 않았습니다.
uploadImageAfterTransaction과updateProblemWithImage모두@Transactional메서드 내에서 호출되므로 여전히 같은 트랜잭션에 참여합니다.다음과 같은 방식으로 트랜잭션을 완전히 분리하는 것을 권장합니다:
@Transactional public ProblemDetailResponse createProblem(ProblemCreateRequest requestDto, MultipartFile image, AuthUser authUser) { User user = userDomainService.getUserById(authUser.getId()); Problem problem = ProblemCreateRequest.toEntity(requestDto, user); Problem savedProblem = problemDomainService.createProblem(problem); - // 문제 이미지 있다면? - if (image != null && !image.isEmpty()) { - String imageUrl = uploadImageAfterTransaction(image, savedProblem.getId()); - updateProblemWithImage(savedProblem.getId(), imageUrl); - } return ProblemDetailResponse.from(savedProblem); } +@Transactional +public ProblemDetailResponse createProblemWithImage(ProblemCreateRequest requestDto, MultipartFile image, AuthUser authUser) { + ProblemDetailResponse response = createProblem(requestDto, null, authUser); + + if (image != null && !image.isEmpty()) { + String imageUrl = uploadImageAfterTransaction(image, response.id()); + updateProblemWithImage(response.id(), imageUrl); + } + + return response; +}🤖 Prompt for AI Agents