都說hibernate的二級緩存用處比較大,在一年前的一個項目時就打算使用hibernate的二級緩存,但時常碰到髒讀又沒時間進行分析和處理,後來決定項目取消了對二級緩存的使用。 前不久公司發錢請了紅帽的高級架構師進行培訓,再三強調二級緩存的做用。。。今天終於下定決心進行了一些測試。首先進行hibernate的二級緩存配置
第一步:.hibernate.cfg.xml 配置文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="GoingOA">
<property name="hibernate.bytecode.use_reflection_optimizer">
false
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/goingTest
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<!-- 設置查詢緩存-->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- 設置二級緩存-->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</property>
<mapping resource="com/going/oa/model/Attachment.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第二步:定義Attachment.hbm.xml 在xml開始部分定義二級緩存
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2009-6-25 14:23:21 by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping package="com.going.oa.model">
<class name="Attachment" table="OA_ATTACHMENT" >
<cache usage="read-write"/>
<id name="id" type="string">
<column name="ATTACHEMENT_ID" length="36" />
<generator class="uuid" />
</id>
<property name="p_w_uploadName" type="string">
<column name="ATTACHMENT_NAME" length="128" not-null="false" >
<comment>附件名稱</comment>
</column>
</property>
<property name="p_w_uploadPath" type="string">
<column name="ATTACHMENT_PATH" not-null="false" length="256" >
<comment>附件全路徑</comment>
</column>
</property>
<property name="p_w_uploadSize" type="string">
<column name="ATTACHMENT_SIZE" default="0" >
<comment>附件大小</comment>
</column>
</property>
<property name="createTime" type="string">
<column name="UPLOAD_TIME" length="32">
<comment>上傳時間</comment>
</column>
</property>
<property name="createId" type="string">
<column name="CREATE_Id" length="32">
<comment>建立人工號</comment>
</column>
</property>
</class>
</hibernate-mapping>
第三步:定義ehcache.xml文件
<ehcache>
<diskStore path="c:\\ehcache\"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<cache name="com.going.oa.model.Attachment"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
</ehcache>