Conversation
There was a problem hiding this comment.
Code Review
This pull request modifies the CandidateVoteRepository by removing the clearAutomatically = true attribute from the @Modifying annotation on the deleteAllByPromiseId method. While this change prevents LazyInitializationException by maintaining the persistence context, it introduces a risk of data inconsistency where the application state may not reflect the database deletions. The feedback suggests verifying that subsequent transaction logic does not rely on stale entities and recommends explicit re-fetching if data integrity is critical.
| boolean existsByUserIdAndPromiseId(Long userId, Long promiseId); | ||
|
|
||
| @Modifying(clearAutomatically = true, flushAutomatically = true) | ||
| @Modifying(flushAutomatically = true) |
There was a problem hiding this comment.
clearAutomatically = true 옵션을 제거하면 벌크 삭제 연산 후에도 영속성 컨텍스트가 유지되어 LazyInitializationException을 방지할 수 있지만, 영속성 컨텍스트와 데이터베이스 간의 데이터 불일치(Stale State) 문제가 발생할 수 있는 점에 유의해야 합니다. 특히 Promise 엔티티가 CandidateVote 엔티티들과 @OneToMany 관계를 맺고 있고 해당 컬렉션이 이미 영속성 컨텍스트에 로드되어 있다면, DB에서 삭제된 데이터가 메모리상의 컬렉션에는 여전히 남아있게 됩니다. 이 트랜잭션의 남은 로직에서 해당 컬렉션을 참조하거나 삭제된 엔티티를 다시 조회하지 않는지 확인이 필요하며, 만약 데이터 정합성이 중요하다면 연산 후 필요한 엔티티를 명시적으로 다시 조회하는 방안을 고려해 보시기 바랍니다.
🔍 관련 이슈
✅ 작업 분류
✨ 작업 내용
👥 전달사항
문제 원인
재투표 로직에서는 기존 투표를 삭제한 뒤 약속 상태를 재투표 상태로 변경합니다.
그런데 deleteAllByPromiseId()에 아래 옵션이 설정되어 있었습니다.
clearAutomatically = true는 벌크 delete 실행 후 JPA 영속성 컨텍스트를 비웁니다. 이 때문에 같은 트랜잭션 안에서 이미 조회해둔 promiseMember가 detached 상태가 되었고,
promiseMember.getPromise()의 지연 로딩 프록시를 초기화할 수 없어 아래 예외가 발생했습니다.
Could not initialize proxy [Promise] - no session
✅ 체크리스트
💡 배운 것 / 시도한 것 / 고민한 점