-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
20250219/spring transaction #148
base: main
Are you sure you want to change the base?
Conversation
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.
코멘트 몇개 드렸는데 참고해보세요 고생하셨습니다~
|
||
## `@Transactional(propagation = Propagation.REQUIRES_NEW)`를 써보며 | ||
|
||
이번에 Spring에서 제공하는 `@Transactional`로 트랜잭션을 선언하고, 분리도 해보았다. 의도대로 동작하지 않는 바람에 오히려 많은 것을 배울 수 있었다. 이 글은 아래와 같은 흐름으로 진행된다. |
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.
분리했다는 내용에서 뭘 분리했는지가 불분명해요 좀 더 자세히 작성해주시면 좋겠네용
트랜잭션은 ‘작업의 한 단위’라고 하는데, 쉽게 말해서 **DB 읽기와 쓰기 여러 개를 논리적으로 묶어놓은 것**을 말한다. | ||
**모두 반영하거나 혹은 모두 반영하지 않거나 두 결과만 있다.** (all or nothing) | ||
|
||
트랜잭션 범위는 **커넥션(Connection) 기준**이다. |
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.
트랜잭션의 범위는 커넥션 기준이다 라는 내용은 정확하지 않은 내용입니다 하나의 커넥션으로 2개 이상의 트랜잭션을 실행할 수 있거든요 트랜잭션의 범위에 대해 설명하고싶다면 데이터베이스에서 트랜잭션을 어떻게 설정하는지 찾아보시고, 그에 대응하는 JDBC 코드에 대해 살펴보시면 좋겠어요
아래 예제코드를 잘 작성해두셔서 그냥 알려드리면 setAutoCommit(false) 를 하는게 트랜잭션의 시작이고, commit/rollback 이 트랜잭션의 종료예요 이걸 범위는 커넥션단위다 라고 표현하는거랑 같게 생각하시면 안돼요~
|
||
// ✅ 알람 저장 및 전송 | ||
Optional<Long> alarmId = alarmService.saveAlarmIfNecessary(vehicleInformation.getId(), updatedTotalDistance); | ||
alarmService.sendAlarm(alarmId); |
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.
이게... 이 내용까지 반영하면 글을 많이 수정하셔야할 것 같아서 조심스럽지만... sendAlarm 이 IO 가 발생하는 부분같은데요
얘가 실패한다고 롤백될 필요가 없으니 트랜잭션을 분리하신 것 같은데 이 경우는 sendAlarm에다가 별도의 @transactional 을 붙이는것보단 IO 가 발생하는 코드를 트랜잭션 바깥으로 꺼내는게 더 좋은 해결법입니다 아래와 같은 모양이 되어야돼요
keyOff();
alarmService.sendAlarm();
다만 아래에서 이벤트를 통해 트랜잭션에서 IO 를 분리하는걸 소개하고 있는건 좋은 내용이네요
return Optional.of(alarmId); | ||
} | ||
} | ||
return Optional.empty(); |
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.
이 예제코드에서 이 return 문은 없어도 되겠네요
No description provided.