EntityManager & PersistenceContext

Woozy9ucci·2023년 12월 14일
0
post-thumbnail

request -> emf 에서 em 생성(with영속성컨텍스트) -> (DB연결이 꼭 필요한 시점까지 커넥션을 얻지 않음)보통 트랜잭션 시작 시 커넥션풀 ->

엔티티에는 네가지 상태가 존재
1. 비영속 (new/transient) = 영속성 컨텍스트와 전혀 관계 X
2. 영속 (managed) = 영속성 컨텍스트에 저장된 상태
3. 준영속 (detached) = 영속성 컨텍스트에 저장되었다가 분리된 상태 (머지가능)
4. 삭제 (removed) = 삭제된 상태

1 비영속 -> em.persist()호출 전

2 영속 -> (em.find() or, jpql로 조회한 엔티티..)

3 준영속 -> (em.detach(), em.clear(), em.close()..)
비영속과 준영속의 차이 준영속은 경험이 있기때문에 반드시 식별자보유 but 비영속은 모름
머지도 결국 새로운 entity를 영속화 하는 것

4 삭제 -> entity가 삭제 된 상태. 엔티티가 삭제 됐음을 인지하고 있다. DB에 반영되는 것은 영속과 삭제상태.

요청 -> EntityManagerFactory.createEntityManager() -> tx.begin() -> tx.commit()
(영속성 컨텍스트는 Transaction의 종속적)
Flush는 영속성 컨텍스트를 비우지 않는다.
but tx.commit() 해도 영속성컨텍스트가 살아있는이유 -> Spring.Jpa OSIV 설정

참고: 엔티티 상태 : https://trillium.tistory.com/m/135

dirty Checking 시점
및 flush와 commit 명확한 차이
https://velog.io/@tomato2532/JPA-JPA-Entity-2-%EC%98%81%EC%86%8D%EC%84%B1-%EC%BB%A8%ED%85%8D%EC%8A%A4%ED%8A%B8Persistence-Context

과정
https://hogwart-scholars.tistory.com/18#%40Transactional%EC%99%80%20Dirty%20Checking-1

OSIV
https://ssdragon.tistory.com/116
https://hstory0208.tistory.com/entry/SpringJPA-OSIV-%EC%A0%84%EB%9E%B5%EC%9D%B4%EB%9E%80-%EC%96%B8%EC%A0%9C-%EC%82%AC%EC%9A%A9%ED%95%B4%EC%95%BC-%ED%95%A0%EA%B9%8C

https://hungseong.tistory.com/74 with readOnly

0개의 댓글

관련 채용 정보