Flush

전승원·2020년 12월 20일
0

강의 출처- 인프런 김영한 강사님
https://www.inflearn.com/course/ORM-JPA-Basic

What is 'Flush'?

Synchronize datas which happend in the persistence context with the ones in database. In other word, making queries inside the 'Write Behind SQL Store' get carried to the database.

How to make 'flush'

1. em.flush() - manually calling flush

Member member = new Member(110L, "flush");
            em.persist(member);
            
            em.flush();

            System.out.println("============================");
            // not execute query before transaction commit.

            tx.commit();

When you wanna see the query being executed before commit, you can do em.flush() liks this.

'Flush' doesn't mean clearing the Persistence Context's 1st Cache. It just makes the queries that are 'stored behind write' carried to the database

2. em.commit() - automatically called

flush is called by default when commit transaction is called.

3. JPQL query executed - automatically called

flush is called by default when JPQL query is executed inside the same transaction

em.persist(memberA);
em.persist(memberB);
em.persist(memberC);

List<Member> resultList = em.createQuery
("Select m from Member m", Member.class).getResultList();

In case like code above, there's possibility that resultList can be null because of the 'write behind', so Jpa ensures by default 'flush is executed' if there's JPQL Query inside transaction,

profile
No pleasure, No gain

0개의 댓글

Powered by GraphCDN, the GraphQL CDN