配置一:
hibernate.cfg.xml文件中增加
<!--開啟二級緩存-->
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
配置二:
工程項目src文件下新建一個ehcache.xml文件,其內容為
<!--開啟二級緩存-->
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="180" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
</ehcache>
配置三:
為了緩存某類的對象,其hbm文件中需添加<cache usage="read-only"/>屬性例如:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.vogue.bbsphoto.entity.Forum"
table="cdb_forums">
<cache usage="read-only"/>
<id name="ID" column="fid" unsaved-value="null">
<generator class="increment" />
</id>
<property name="name" column="name" type="string" />
<property name="type" column="type" type="string" />
</class>
</hibernate-mapping>
配置四:
為了使用查詢緩存,Query必須設置cacheable為true,query.setCacheable(true);
例如dao父類中用于hql查詢的方法修改后為:
/**
* 執行hql語句的查詢
* @param sql
* @return
*/
public List executeQuery(String hql){
List list = new ArrayList();
Session session = HibernateSessionFactory.currentSession();
Transaction tx = null;
Query query = session.createQuery(hql);
query.setCacheable(true);
try {
tx = session.beginTransaction();
list = query.list();
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
HibernateSessionFactory.rollbackTransaction(tx);
} finally {
HibernateSessionFactory.closeSession();
}
return list;
}
實驗結果:
補 充一下:當要緩存的對象處于級聯關系中時。如果和他存在級聯關系的對象都有屬性 <cache usage="read-only"/>那么,在第一次get后該對象所處的對象圖中的所有對象都會保存到hibernate的二級緩存中,在第二 次get該對象時,直接從二級緩存中找到所有級聯的對象;如果其中某個級聯對象沒有<cache usage="read-only"/>屬性,則不會被保存到二級緩存中,以后每次get時仍然會執行sql去數據庫中找該級聯對象。
</div>
來自:http://jdskyy.javaeye.com/blog/323207
hibernate.cfg.xml文件中增加
<!--開啟二級緩存-->
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
配置二:
工程項目src文件下新建一個ehcache.xml文件,其內容為
<!--開啟二級緩存-->
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="180" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
</ehcache>
配置三:
為了緩存某類的對象,其hbm文件中需添加<cache usage="read-only"/>屬性例如:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.vogue.bbsphoto.entity.Forum"
table="cdb_forums">
<cache usage="read-only"/>
<id name="ID" column="fid" unsaved-value="null">
<generator class="increment" />
</id>
<property name="name" column="name" type="string" />
<property name="type" column="type" type="string" />
</class>
</hibernate-mapping>
配置四:
為了使用查詢緩存,Query必須設置cacheable為true,query.setCacheable(true);
例如dao父類中用于hql查詢的方法修改后為:
/**
* 執行hql語句的查詢
* @param sql
* @return
*/
public List executeQuery(String hql){
List list = new ArrayList();
Session session = HibernateSessionFactory.currentSession();
Transaction tx = null;
Query query = session.createQuery(hql);
query.setCacheable(true);
try {
tx = session.beginTransaction();
list = query.list();
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
HibernateSessionFactory.rollbackTransaction(tx);
} finally {
HibernateSessionFactory.closeSession();
}
return list;
}
實驗結果:
補 充一下:當要緩存的對象處于級聯關系中時。如果和他存在級聯關系的對象都有屬性 <cache usage="read-only"/>那么,在第一次get后該對象所處的對象圖中的所有對象都會保存到hibernate的二級緩存中,在第二 次get該對象時,直接從二級緩存中找到所有級聯的對象;如果其中某個級聯對象沒有<cache usage="read-only"/>屬性,則不會被保存到二級緩存中,以后每次get時仍然會執行sql去數據庫中找該級聯對象。
</div>
來自:http://jdskyy.javaeye.com/blog/323207
Hibernate二級緩存也稱為進程級的緩存或SessionFactory級的緩存。二級緩存是全局緩存,它可以被所有的session共享。二級緩存的生命周期和SessionFactory的生命周期一致,SessionFactory可以管理二級緩存。
二級緩存的配置使用:
1.在crc下創建echcache.xml文件,其內容如下:
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false" /> </ehcache>
maxElementsInMemory屬性用于指定緩存中最多可放多少個對象。
eternal屬性指定緩存是否永久有效。
timeToIdleSeconds屬性指定緩存多久未被使用便清理掉。
timeToLiveSeconds屬性指定緩存的生命長度。
diskPersistent屬性指定緩存是否被持久化到硬盤中,保存路徑由<diskStore>標簽指定。
2.修改hibernate.cfg.xml文件開啟二級緩存。
<hibernate-configuration> <session-factory> <!-- 開啟二級緩存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 設置緩存提供者 --> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <mapping resource="cn/ineeke/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration>
3.指定哪些實體類使用緩存。經過第二步緩存是啟用了,但是并沒有被使用。它不會去自動把所有的實體都進行緩存了,而是需要手動指定哪個實體需要緩存,以及其緩存的策略。這里有兩種方式,第一種是修改要使用緩存的實體的映射文件。如在User.hbm.xml中使用<cache>標簽啟用。
<hibernate-mapping> <class name="cn.ineeke.entity.User" table="t_user"> <cache usage="read-only"/> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping>
第二種方式是在hibernate.cfg.xml中使用<class-cache>標簽指定實體類并啟用。
<hibernate-configuration> <session-factory> <!-- 開啟二級緩存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 設置緩存提供者 --> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <mapping resource="cn/ineeke/entity/User.hbm.xml"/> <!-- 指定哪些實體需要使用二級緩存 --> <class-cache class="cn.ineeke.entity.User" usage="read-only"/> </session-factory> </hibernate-configuration>
<calss-cache>標簽中的class屬性指定要對哪個實體進行緩存,而usage屬性與<cache>標簽的相同,都指的是緩存策略,此值依實際需要而定,默認采用read-only。