코드 그라데이션

flush와 준영속 상태 본문

Spring/JPA 공부

flush와 준영속 상태

완벽한 장면 2023. 8. 14. 19:06

플러시(flush)

: 영속성 컨텍스트의 변경내용을 데이터베이스에 반영

 

 

플러시 발생

  • 변경 감지
  • 수정된 엔티티 쓰기 지연 SQL 저장소에 등록
  • 쓰기 지연 SQL 저장소의 쿼리를 데이터베이스에 전송(등록, 수정, 삭제 쿼리)

 

영속성 컨텍스트를 플러시하는 방법

• em.flush() - 직접 호출
• 트랜잭션 커밋 
- 플러시 자동 호출
• JPQL 쿼리 실행 
- 플러시 자동 호출

 

-> em.flush를 해도 1차캐시는 유지가 된다. 그냥 쓰기지연 저장소에 쌓인 것들과 바귄 것들이 저장된다고 보면 된다.

 

JPQL 쿼리 실행 시 플러시가 자동으로 호출되는 이유

 

플러시 모드 옵션

 

플러시는!!!!!!!!!!!

 

준영속 상태

 

준영속 상태로 만드는 방법

 

실습

저장을 미리 하고싶으면(커밋시점 전에)

public class JpaMain {
  public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
    EntityManager em = emf.createEntityManager();

    EntityTransaction tx = em.getTransaction();
    tx.begin();

    try {

      Member member = new Member(200L, "member200");
      em.persist(member); // 여기서 영속성 컨텍스트에 저장이 되고
      //미리 보고싶으면
      em.flush();//강제로 호출

      System.out.println("===================");
      tx.commit(); // 원래는 여기서 저장이 되는데
    } catch (Exception e) {
      tx.rollback();
    } finally {
      em.close();
    }
    emf.close();
  }
}

 

 

준영속

public class JpaMain {
  public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
    EntityManager em = emf.createEntityManager();

    EntityTransaction tx = em.getTransaction();
    tx.begin();

    try {

      //영속 상태
      Member member = em.find(Member.class, 150L);
      member.setName("AAAAA");
      em.detach(member); // 준영속 상태로 만듦

      System.out.println("===================");
      tx.commit(); 
    } catch (Exception e) {
      tx.rollback();
    } finally {
      em.close();
    }
    emf.close();
  }
}

select 쿼리만 나오고 update 쿼리는 나오지 않음.

23:55:48.853 [main] DEBUG org.hibernate.SQL - 
    select
        member0_.id as id1_0_0_,
        member0_.name as name2_0_0_ 
    from
        Member member0_ 
    where
        member0_.id=?
Hibernate: 
    select
        member0_.id as id1_0_0_,
        member0_.name as name2_0_0_ 
    from
        Member member0_ 
    where
        member0_.id=?
23:55:48.859 [main] DEBUG org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl - On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified
23:55:48.871 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Resolving attributes for [inflearn.exjpa.Member#150]
23:55:48.872 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Processing attribute `name` : value = ZZZAAA
23:55:48.873 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Attribute (`name`)  - enhanced for lazy-loading? - false
23:55:48.874 [main] DEBUG org.hibernate.engine.internal.TwoPhaseLoad - Done materializing entity [inflearn.exjpa.Member#150]
23:55:48.879 [main] DEBUG org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader - Done entity load : inflearn.exjpa.Member#150
===================
23:55:48.887 [main] DEBUG org.hibernate.engine.transaction.internal.TransactionImpl - committing
23:55:48.889 [main] DEBUG org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl - Initiating JDBC connection release from afterTransaction
23:55:48.889 [main] DEBUG org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl - Initiating JDBC connection release from afterTransaction
23:55:48.891 [main] DEBUG org.hibernate.internal.SessionFactoryImpl - HHH000031: Closing
23:55:48.892 [main] DEBUG org.hibernate.type.spi.TypeConfiguration$Scope - Un-scoping TypeConfiguration [org.hibernate.type.spi.TypeConfiguration$Scope@367795c7] from SessionFactory [org.hibernate.internal.SessionFactoryImpl@78f5c518]
23:55:48.893 [main] DEBUG org.hibernate.service.internal.AbstractServiceRegistryImpl - Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries
23:55:48.893 [main] INFO org.hibernate.orm.connections.pooling - HHH10001008: Cleaning up connection pool [jdbc:h2:tcp://localhost/~/test]
23:55:48.894 [main] DEBUG org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries

종료 코드 0(으)로 완료된 프로세스

 

 

728x90
Comments