[oneseo] 성적 계산 싱글톤 필드 동시성 문제 ThreadLocal 사용으로 개선 #229
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
개요
성적 계산 클래스(CalculateGradeService) 싱글톤 클래스 필드 동시성 문제를 ThreadLocal을 사용하여 개선하였습니다.
본문
성적 계산시 개별학기 환산점은 계산 메서드 여러 곳에서 사용되기 때문에 필드로 빼서 구현하게 되었습니다.
하지만 이러한 구조로 인해 싱글톤으로 관리되는 빈 특성상 여러 스레드가 필드를 공유하게 되고, 동시성 문제가 발생하였습니다.
#187 이러한 문제는 계산 메서드 시작 전에 필드를 초기화 하는 방식으로 해결한 작업이 반영되었습니다.
하지만 이러한 대처는 비슷한 방식의 동시성 문제를 야기할 수 있을 것이라 생각되어 불안전한 방식입니다.
그래서 현재 구조를 크게 변경하지 않으면서 안정성을 보장할 수 있는 방법인 ThreadLocal을 도입하여 문제를 개선해보았습니다.
ThreadLocal은 여러 스레드가 공유하는 필드를 각각의 스레드의 저장공간에 저장하여 해당 필드를 ThreadSafe하게 보장해주는 기능입니다.
기존 코드 구조를 크게 변경하지 않고 적용할 수 있어서 괜찮은 방식이라 생각하였습니다.