Hibernate二级缓存的简单示例

来源:互联网 发布:苹果电脑系统安装软件 编辑:程序博客网 时间:2024/05/24 06:28

1. 在hibernate.fcg.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><property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property><property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property><property name="connection.username">oa</property><property name="connection.password">oa</property><property name="connection.pool_size">1</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.Oracle10gDialect</property><property name="show_sql">true</property><property name="format_sql">true</property><property name="hbm2ddl.auto">update</property><!-- 开启二级缓存 --><property name="hibernate.cache.use_second_level_cache">true</property><!-- 设置二级缓存的实现类 --><property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property><!-- Hibernate4以后都封装到org.hibernate.cache.ehcache.EhCacheRegionFactory --><property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property><!-- 指定缓存配置文件位置 --><property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property><!--启动查询缓存,如果想缓存使用findAll、list、iterator、createCriteria、createQuery等方法获得的数据结果集,必须配置此项--><property name="hibernate.cache.use_second_level_cache">true</property><mapping resource="com/huey/entity/mapping/Person.hbm.xml" /></session-factory></hibernate-configuration>

2. 导入二级缓存所需的jar包。以EHCache为例,需导入Hibernate项目路径下的lib\optional\ehcache中的jar包,同时EHCache还依赖于commons-logging和backport-util-concurrent这两个jar包。


3. 对于EHCache缓存,它还需要一个ehcache.xml配置文件:

<?xml version="1.0"?><ehcache><diskStore path="java.io.tmpdir"/><defaultCachemaxElementsInMemory="10000"eternal="false"overflowToDisk="true"timeToIdleSeconds="120"timeToLiveSeconds="120"diskPersistent="false"/></ehcache>
配置文件中属性的说明:

maxElementsInMemory:设置缓存中最多可放入多少个对象;
eternal:设置缓存是否永久有效;
overflowToDisk:设置当内存中缓存的记录达到maxElementsInMemory时是否被持久化到硬盘中,保存路径由<diskStore.../>元素指定。
timeToIdleSeconds:设置缓存的对象多少秒没有被使用就会被清理掉;
timeToLiveSeconds:设置缓存的对象在过期之前可以缓存多少秒;
diskPersistent:设置在虚拟机重启时是否进行磁盘存储。


4. 设置对哪些实体类、实体的哪些集合属性启用二级缓存。有两种方式:

1) 在持久化映射文件的<class .../>元素或<set.../>、<list .../>等集合元素内使用<cache .../>元素指定缓存策略。

2) 在hibernate.cfg.xml文件中使用<class-cache .../>或<collection-cache .../>元素对指定的持久化类、集合属性启用二级缓存。

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.huey.entity"><class name="Person" table="tab_person"><!-- 指定持久化类Person开启二级缓存,并指定缓存策略为只读策略 --><cache usage="read-only"/><id name="id" column="id"><generator class="sequence"><param name="sequence">PERSON_ID_SEQUENCE</param></generator></id><property name="name" column="name" /><property name="gender" column="gender"/><property name="birthday" column="birthday"/></class></hibernate-mapping>

5. 测试用例:

package com.huey.test;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.Test;import com.huey.entity.Person;public class PersonTest {private static SessionFactory sf;static {Configuration configuration = new Configuration().configure("hibernate.cfg.xml");ServiceRegistryBuilder srb = new ServiceRegistryBuilder().applySettings(configuration.getProperties());sf = configuration.buildSessionFactory(srb.buildServiceRegistry());}@Testpublic void testPerson() throws Exception {Session session = sf.openSession();session.beginTransaction();List<?> people = session.createQuery("from Person").list();for (Object object : people) {Person person = (Person)object;System.out.println(person.getName());}session.getTransaction().commit();session.close();Session session2 = sf.openSession();session2.beginTransaction();Person person = (Person)session2.get(Person.class, 1);System.out.println(person.getName());session2.getTransaction().commit();session2.close();}}
可以发现,启用二级缓存后,即使session关闭之后,第二个查询仍不需发送SQL查询语句。

原创粉丝点击