@Transactional(readOnly = true)를 적용했을 때와 아무 어노테이션을 적용하지 않았을 때 차이 #209
Replies: 3 comments 3 replies
-
중복 디스커션입니다. |
Beta Was this translation helpful? Give feedback.
-
@transactional(readOnly = true) 을 사용하게 된다면, DB 트랜잭션이 읽기 전용 모드로 설정 되니다. 이를 통해 다음과 같은 이점을 얻을 수 있습니다.
|
Beta Was this translation helpful? Give feedback.
-
@Service
public class ReservationService {
private final ReservationRepository reservationRepository;
public ReservationService(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}
public String getMemberName(Long id) {
Reservation reservation = reservationRepository.findById(id).orElseThrow(...);
return reservation.getMember().getName(); // LazyInitializationException
}
} OSIV 옵션이 false라고 가정했을 때 영속성 컨텍스트는 트랜잭션이 종료될 때 닫히게 되어 영속성 컨텍스트에 관리되고 있는 엔티티는 준영속 상태가 됩니다.
번외
따라서 트랜잭션 내에서 강제로 JpaTransactionManager.doBegin() -> HibernateJpaDialect.beginTransaction() -> HibernateJpaDialect.prepareFlushMode() |
Beta Was this translation helpful? Give feedback.
-
.
Beta Was this translation helpful? Give feedback.
All reactions