1. 程式人生 > >Hibernate二級緩存

Hibernate二級緩存

nta hat unique there 文件 hibernate cto owin ces

 1 @org.junit.Test
 2     public void testHibernateSecondLevelCache(){
 3         Employee employee=(Employee) session.get(Employee.class, 7499);
 4         System.out.println(employee.getName());
 5         
 6         transaction.commit();
 7         session.close();
 8         session = sessionFactory.openSession();
9 transaction = session.beginTransaction(); 10 11 Employee employee2=(Employee) session.get(Employee.class, 7499); 12 System.out.println(employee2.getName()); 13 }

現在這 這段代碼就是兩個session。會發送兩條sql語句。

一:hibernate二級緩存應用

1:加入jar包

2:加入ehcache.xml

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home 
- User‘s home directory user.dir - User‘s current working directory java.io.tmpdir - Default temp file path --> <diskStore path="java.io.tmpdir"/> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required
for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <!--Predefined caches. Add your cache configuration settings here. If you do not have a configuration for your cache a WARNING will be issued when the CacheManager starts The following attributes are required for defaultCache: name - Sets the name of the cache. This is used to identify the cache. It must be unique. maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <!-- Sample cache named sampleCache1 This cache contains a maximum in memory of 10000 elements, and will expire an element if it is idle for more than 5 minutes and lives for more than 10 minutes. If there are more than 10000 elements it will overflow to the disk cache, which in this configuration will go to wherever java.io.tmp is defined on your system. On a standard Linux system this will be /tmp" --> <cache name="sampleCache1" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <!-- Sample cache named sampleCache2 This cache contains 1000 elements. Elements will always be held in memory. They are not expired. --> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> --> <!-- Place configuration for your caches following --> </ehcache>

3:在hibernate.cfg.xml文件中配置需要緩存的配置。

<!-- 配置啟用hibernate的二級緩存 -->
        <property name="cache.use_second_level_cache">true</property>
        <!-- 配置hibernate二級緩存使用的產品 -->
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <!-- 配置對哪些類使用 hibernate 的二級緩存  。在mapping的後面 -->
        <class-cache usage="read-write" class="com.hq.entities.Employee"/>

或者在Employee的 hbm.xml文件中加上

<cache usage="read-write"/>

**:對集合進行二級緩存的配置:

     <class-cache usage="read-write" class="com.hq.entities.Employee"/>
        <class-cache usage="read-write" class="com.hq.entities.Department"/>
        <collection-cache usage="read-write" collection="com.hq.entities.Department.emps"/>
 

或者在Department.hbm.xml和Employee.hbm.xml中加上

 <cache usage="read-write"/>

然後在集合中加上cache

<set name="emps" table="GG_EMP" inverse="true" lazy="true">
            <cache usage="read-write"/>
            <key>
                <column name="DEPT_ID" />
            </key>
            <one-to-many class="com.hq.entities.Employee" />
        </set>

***. 查詢緩存: 默認情況下, 設置的緩存對 HQL 及 QBC 查詢時無效的, 但可以通過以下方式使其是有效的

I. 在 hibernate 配置文件中聲明開啟查詢緩存

<property name="cache.use_query_cache">true</property>

II. 調用 Query 或 Criteria 的 setCacheable(true) 方法

III. 查詢緩存依賴於二級緩存

Hibernate二級緩存