read-only = true

spring-jpa(entity manager, transaction manager)

- HibernateEntityManagerFactory가 session manager를 감싸고 있음

<-> hibernate adapter(session manager, transaction manager) 

<-> jdbc(connection, begin transaction, commit, rollback)

<-> mysql(begin transaction, commit, rollback)

spring

hibernate
  • 하이버네이트에는 read-only 세션이 없다. spring 에서 하는일은 hibernate flush 모드를 FLUSH_NEVER 로 설정한다.  HibernateAccessor 에 정의되어있음
  • HQL 쿼리 호출이 session state 을 DB에 저장하는 flush를 일으키지 않는다. 성능향상이 된다.
  • Hibernate는 트랜젝션 commit 전에 flush 하지 않는다.
  • 명시적인 flush()는 가능해서, spring이 Connection.setReadOnly() 이 유용하다.
    -> 문서에는 가능하다고 했는데, 테스트해보면 안됨..flush() 하면 에러남 

flush VS commit

  • flush : session state 와 DB를 sync하는것
  • commit : flush + end of the unit of work
  • flush를 호출하지 않으면 first-level cache에서 out of memory가 일어남

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
     
     
    for int i=0; i<100000; i++ ) {
        Customer customer = new Customer(.....);
        session.save(customer);
        if ( i % 20 == 0 ) { //20, same as the JDBC batch size
            //flush a batch of inserts and release memory:
            session.flush();
            session.clear();
        }
    }
     
    tx.commit();
    session.close();

logback 설정 추가

  • 아래 설정을 추가하면 connection obtain, release를 볼수 있다.
  • @Transaction과 jdbc connection의 연관관계를 볼수 있다

logger('org.springframework.orm.jpa.JpaTransactionManager', DEBUG)
logger('org.hibernate.engine.jdbc.internal', DEBUG)

DEBUG 2015-03-20 06:15:14 [http-bio-19010-exec-8] o.s.orm.jpa.JpaTransactionManager.doBegin - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@55a8aa6e] for JPA transaction
DEBUG 2015-03-20 06:15:14 [http-bio-19010-exec-8] o.h.e.j.i.LogicalConnectionImpl.obtainConnection - Obtaining JDBC connection

DEBUG 2015-03-20 06:15:14 [http-bio-19010-exec-8] o.s.orm.jpa.JpaTransactionManager.processCommit - Initiating transaction commit
DEBUG 2015-03-20 06:15:14 [http-bio-19010-exec-8] o.s.orm.jpa.JpaTransactionManager.doCommit - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@55a8aa6e]
DEBUG 2015-03-20 06:15:14 [http-bio-19010-exec-8] o.s.orm.jpa.JpaTransactionManager.doCleanupAfterCompletion - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@55a8aa6e] after transaction
DEBUG 2015-03-20 06:15:14 [http-bio-19010-exec-8] o.h.e.j.i.LogicalConnectionImpl.releaseConnection - Releasing JDBC connection
DEBUG 2015-03-20 06:15:14 [http-bio-19010-exec-8] o.h.e.j.i.LogicalConnectionImpl.releaseConnection - Released JDBC connection

 

http://www.codeinstructions.com/2009/04/read-only-transactions-with-spring-and.html

http://stackoverflow.com/questions/14581865/hibernate-commit-and-flush


'mysql' 카테고리의 다른 글

hibernate flush VS commit  (0) 2016.12.20
mysql filesort  (0) 2016.12.20
mysql btree structure  (0) 2016.12.20
cardinality  (0) 2016.12.20

+ Recent posts