2016-06-08 2 views
1

2 레벨 캐시 누락 또는 적중에 대한 정보를 수집하기 위해 최대 절전 모드 통계를 사용합니다. 테스트 모듈을 실행할 때 두 번째 레벨의 미스가 있지만 조회수가없는 이유는 무엇입니까? 예제에서 두 번째 레벨 캐시가 작동하지 않는 이유는 무엇입니까?간단한 예제에서 Hibernate + ehcache 2 차 레벨 캐시 미스

<?xml version="1.0" encoding="utf-8"?> 
    <!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
     <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.100.0:1521:rrr</property> 
     <property name="hibernate.connection.username">ora</property> 
     <property name="hibernate.connection.password">lll</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> 
     <property name="hibernate.default_schema">ora</property> 
     <property name="hibernate.current_session_context_class">thread</property> 
     <property name="hibernate.generate_statistics">true</property> 
     <property name="show_sql">false</property> 
     <property name="format_sql">false</property> 
     <property name="use_sql_comments">false</property> 

     <property name="hibernate.cache.use_second_level_cache">true</property> 
     <property name="hibernate.cache.use_query_cache">true</property> 
     <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> 
     <property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property> 


     <mapping class="com.ric.bill.model.bs.Lst"></mapping> 
     <mapping class="com.ric.bill.model.bs.LstTp"></mapping> 

    </session-factory> 
</hibernate-configuration> 

그리고 다음 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="true"> 

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

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

    <cache name="billCache" maxEntriesLocalHeap="10000" 
     eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0"> 
     <persistence strategy="localTempSwap" /> 
    </cache> 

    <cache name="org.hibernate.cache.internal.StandardQueryCache" 
     maxEntriesLocalHeap="5000" eternal="false" timeToLiveSeconds="120"> 
     <persistence strategy="localTempSwap" /> 
    </cache> 

    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache" 
     maxEntriesLocalHeap="5000" eternal="true"> 
     <persistence strategy="localTempSwap" /> 
    </cache> 
</ehcache> 

, 다음의 pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.journaldev.hibernate</groupId> 
<artifactId>HibernateEHCacheExample</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<description>Hibernate Secondary Level Cache Example using EHCache implementation</description> 

<dependencies> 
    <!-- Hibernate Core API --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>5.1.0.Final</version> 
    </dependency> 
    <!-- EHCache Core APIs --> 
    <!-- http://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core --> 
    <dependency> 
     <groupId>net.sf.ehcache</groupId> 
     <artifactId>ehcache-core</artifactId> 
     <version>2.6.11</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-ehcache</artifactId> 
     <version>5.2.0.Final</version> 
    </dependency> 

    <!-- EHCache uses slf4j for logging --> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-simple</artifactId> 
     <version>1.7.5</version> 
    </dependency> 
</dependencies> 
</project> 

그리고이 간단한

나는 옆에있는 hibernate.cfg.xml을 법인 :

package com.ric.bill.model.bs; 

import javax.persistence.Cacheable; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 

import org.hibernate.annotations.Cache; 
import org.hibernate.annotations.CacheConcurrencyStrategy; 

import com.ric.bill.Simple; 

@SuppressWarnings("serial") 
@Entity 
@Table(name = "LIST", schema="BS") 
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="billCache") 
@Cacheable 
public class Lst implements java.io.Serializable, Simple { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "ID", updatable = false, nullable = false) 
    private Integer id; //id 

    @Column(name = "CD", updatable = false, nullable = false) 
    private String cd; //cd 

    @Column(name = "NAME", updatable = false, nullable = false) 
    private String name; //Наименование 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name="FK_LISTTP", referencedColumnName="ID") 
    private LstTp lstTp ; 


    public Integer getId() { 
     return this.id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getCd() { 
     return this.cd; 
    } 
    public void setCd(String cd) { 
     this.cd = cd; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public LstTp getLstTp() { 
     return lstTp; 
    } 
    public void setLstTp(LstTp lstTp) { 
     this.lstTp = lstTp; 
    } 

} 
백45경1천5백15조5백36억9천1백36만3천2백10

내 테스트 모듈 :

public class Test { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     SessionFactory sf = HibernateUtil.getSessionFactory(); 
     Session sess = sf.openSession(); 
     sess.beginTransaction(); 

     for (int a=1401; a<=1452; a++) { 
      Lst lst = (Lst) sess.load(Lst.class, a); 
      System.out.println(lst.getName()); 
     } 
     for (int a=1401; a<=1452; a++) { 
      Lst lst = (Lst) sess.load(Lst.class, a); 
      System.out.println(lst.getName()); 
     } 
     Statistics stats = sf.getStatistics(); 
     printStats(stats, 0); 

     sess.getTransaction().commit(); 

     System.out.println("Complete!"); 

    } 
} 

출력 :

Fetch Count=52 
Second Level Hit Count=0 
Second Level Miss Count=52 
Second Level Put Count=52 
Complete! 

답변

2

레벨 2 캐시는 다른 세션 사이 캐시 엔티티에 사용된다. 여기 당신은 같은 세션에 있기 때문에 최대 절전 모드는 레벨 2 캐시로 갈 필요가 없었습니다. 세션에서 엔티티를 가져올 수있었습니다.

당신이 보았 듯이 104가 아니라 단지 52 회의 미스가 발생합니다. 첫 번째 for 루프 만이 이러한 미스를 생성합니다. 그런 다음 최대 절전 모드로 엔티티를 가져 와서 세션과 레벨 2 캐시에 넣습니다. 두 번째 루프에서는 세션에서 세션을 찾았으며 레벨 2 캐시를 보지 않아도되므로 히트 나 미스가 생성되지 않았습니다.

레벨 2 캐시를 테스트하려면 첫 번째 for 루프의 끝에서 트랜잭션과 세션을 닫고 두 번째 루프 전에 새로운 트랜잭션 (tx 및 session)을여십시오.

+0

정말! 첫 번째 세션을 닫고 다른 세션을 열고 지금 작동합니다. 가져 오기 수 = 52 2 차 수 방문 횟수 = 52 2 차 레벨 누적 횟수 = 52 2 단계 누적 횟수 = 52 완료! – Lev

+0

예 2 수준 캐싱을 사용합니다. 이제 엔티티가 스레드간에 공유됩니다. – Thierry

관련 문제