都说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>