> For the complete documentation index, see [llms.txt](https://ssosso.gitbook.io/study/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ssosso.gitbook.io/study/java/java-orm-jpa.md).

# Java ORM 표준 JPA 프로그래밍

2회독 정리

### OSIV(Open Session In View)

* 영속성컨텍스트 생존범위
* off&#x20;
  * 영속상태 범위 => 트랜잭션 범위(서비스->레포지토리)
  * **트랜잭션 종료할때, 영속성 컨텍스트를 닫고, 데이터베이스 커넥션도 반환**
  * **커넥션 리소스낭비안됨**
* 디폴트 on
  * **API응답이 끝날때 까지 영속성 컨텍스트와 데이터베이스 커넥션이  유지됨**
  * **실시간 트래픽에서는 커넥션풀 낭비가 심해서 비효**

### 영속성 컨텍스트 이점

* 1차캐시
  * em.persist(a) : 영속상태
  * em.find(a) -> **영속성 컨텍스트에서 조회** (1차캐시)&#x20;
* 트랜잭션을 지원하는 쓰기지연
  * persist(a), persist(b) ...&#x20;
  * tx.commit() -> **트랜잭션 커밋 순간에 SQL 실행**
* 더티체킹 (변경감지)
  * 조회된 영속 엔티티 -> 수정 -> update 문이 없어도 데이터 수정
* 지연로딩
