2016-10-17 15 views
1

이 내 의 persistence.xmlHibernate EhCache가 사용되지 않았습니까?

<property name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" /> 

<property name="hibernate.connection.autocommit" value="false"/> 
<property name="hibernate.show_sql" value="true" />    
<property name="hibernate.format_sql" value="true" /> 
<property name="hibernate.generate_statistics" value="true"/> 

<!-- set cache provider --> 
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" /> 

<!-- enable second level cache and query cache --> 
<property name="hibernate.cache.use_second_level_cache" value="true" /> 
<property name="hibernate.cache.use_query_cache" value="true" /> 
<property name="net.sf.ehcache.configurationResourceName" value="/ehcache.xml" /> 

ehcache.xml 마지막으로

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="ehcache.xsd" 
    updateCheck="true" 
    monitoring="autodetect" 
    dynamicConfig="false"> 

    <diskStore path="java.io.tmpdir/ehcache" /> 

    <defaultCache 
     maxEntriesLocalHeap="10000" 
     eternal="false" 
     timeToIdleSeconds="120" 
     timeToLiveSeconds="120" 
     diskSpoolBufferSizeMB="30" 
     maxEntriesLocalDisk="10000000" 
     diskExpiryThreadIntervalSeconds="120" 
     statistics="true"> 
     <persistence strategy="localTempSwap" /> 
    </defaultCache> 

</ehcache> 

주요 방법 (I는 JPA 준수 할)

의 일부입니다
void main() 
{ 
    EntityManagerFactory emf = null;   
    EntityManager em = null; 

    try { 

     emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 

     em = emf.createEntityManager(); 

     Session session = em.unwrap(Session.class); 

     Customer find; 

     Transaction transaction = session.getTransaction(); 

     transaction.begin(); 

     CustomerPk pk = new CustomerPk(); 
     pk.setUidCliente("[email protected]"); 

     find = em.find(Customer.class, pk); 

     find = em.find(Customer.class, pk); 

     transaction.commit(); 

     logger.info("END"); 

    } catch (Exception e) { 
     logger.error(e.getMessage()); 
    } finally { 
     Optional.ofNullable(em).ifPresent(x -> { x.close(); }); 
     Optional.ofNullable(emf).ifPresent(x -> { x.close(); });    
    } 
} 

이제는 CT가 2 레벨 캐시는 전화를 찾아 두 번째 에 'hitted'하지만 기록 통계는 다음과 같은 인쇄 있기 때문에, 아니다해야합니다

20786 nanoseconds spent acquiring 1 JDBC connections; 
20057 nanoseconds spent releasing 1 JDBC connections; 
169837247 nanoseconds spent preparing 1 JDBC statements; 
322972879 nanoseconds spent executing 1 JDBC statements; 
0 nanoseconds spent executing 0 JDBC batches; 
4190446 nanoseconds spent performing 1 L2C puts; 
0 nanoseconds spent performing 0 L2C hits; 
1239520 nanoseconds spent performing 1 L2C misses; 
6351857 nanoseconds spent executing 1 flushes (flushing a total of 1 entities and 0 collections); 
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections) 

당신이 볼 수 있듯이, 0 L2C 안타.

무엇이 문제입니까?

+1

첫 번째 찾기 후에'em.clear()'를 호출 한 @Gab의 대답에 더하여 L1 캐시를 지우고 L2 캐시에서로드를 트리거해야합니다. –

답변

3

두 가져 오기 작업에 동일한 EntityManager를 사용하므로 l1이 이미 일치하므로 l2 캐시에 연결할 필요가 없습니다.

L1 캐시는 엔티티 관리자 상태이므로 EM 수명주기에 바인딩됩니다. L2 캐시는 entityManagerFactory 수준에 있으며 동일한 공장에서 생성 된 모든 EM간에 공유됩니다.

+0

EM을 닫은 후 EMF를 통해 다시 만들었지 만 동일한 찾기를했지만 여전히 L2C 히트 (L2C 1 넣기)가 표시되지 않습니다. 그게 뭐가 잘못 됐어? –

+0

이 모든 것이 동일한 거래 내에서 발생합니까? – Gab

+0

죄송합니다. 내 잘못이었습니다. 첫 번째 찾기 후에 캐시를 퇴출했습니다(). –