Hibernate二级缓存

来源:互联网 发布:tensorflow应用领域 编辑:程序博客网 时间:2024/06/03 18:52
Hibernate第二级缓存是会话工厂的所有会话(Session)对象所使用的公共缓存。 如果您有来自会话工厂的多个会话(Session)对象,就可以操作会话工厂中的第二级缓存的数据。
SessionFactory类用于保存二级缓存数据。 它是所有会话对象的全局,默认情况下是不启用的。
不同厂商提供了二级缓存的实现。
1. EH二级缓存
2. OS二级缓存
3. Swarm二级缓存
4. JBoss二级缓存
每个实现提供不同的缓存使用功能。 有四种方法可以使用二级缓存。
1. 只读:缓存将适用于只读操作。
2. 非严格读写:缓存可用于读写,但一次只能读写。
3. 读写:缓存将用于读写,可以同时使用。
4. 事务处理:缓存将用于事务处理。

下面来看看看二级缓存实现和缓存使用情况。
实现只读非限制读写读写操作EH二级缓存YesYesYesNoOS二级缓存YesYesYesNoSwarm二级缓存YesYesNoNoJBoss二级缓存NoNoNoYes
使用EH缓存的二级缓存示例的额外步骤
1)在hibernate.cfg.xml文件中添加2个配置设置
<property name="hibernate.cache.use_query_cache">true</property><property name="cache.provider_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property><property name="hibernate.cache.use_second_level_cache">true</property>
2) 缓存使用属性可以应用于hbm.xml文件中的类或集合级别,在hbm文件中添加缓存使用情况设置
<cache usage="read-only" />
3) 创建ehcache.xml文件
<?xml version="1.0"?><ehcache><defaultCache maxElementsInMemory="100" eternal="true"/></ehcache>
Hibernate二级缓存示例

项目的整体结构如下:


student.hbm.xml文件如下:
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="demo.Student" table="student"><cache usage="read-only"/><id name="id"><generator class="increment"></generator></id><property name="name"></property></class>

hibernate.cfg.xml文件如下:

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        " http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql://localhost:3306/first_db?serverTimezone=UTC</property>        <property name="connection.username">root</property>        <property name="connection.password">9958</property>        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="show_sql">true</property>        <!-- 开启查询缓存 -->        <property name="hibernate.cache.use_query_cache">true</property>        <!-- 配置二级缓存 -->        <property name="hibernate.cache.use_second_level_cache">true</property>        <!--<property name="cache.provider_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>-->        <!--hibernate4以后改为如下-->        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>        <mapping resource="student.hbm.xml"/>    </session-factory></hibernate-configuration></hibernate-mapping>

ehcache.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="true"         monitoring="autodetect" dynamicConfig="true">    <diskStore path="java.io.tmpdir/ehcache" />    <defaultCache maxEntriesLocalHeap="10000" eternal="false"                  timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"                  maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"                  memoryStoreEvictionPolicy="LRU" statistics="true">        <persistence strategy="localTempSwap" />    </defaultCache>    <cache name="org.hibernate.cache.internal.StandardQueryCache"           maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120">        <persistence strategy="localTempSwap" />    </cache>    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"           maxEntriesLocalHeap="5000" eternal="true">        <persistence strategy="localTempSwap" />    </cache>    <cache name="demo.Student" maxElementsInMemory="100"           eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="200" /></ehcache>
defaultCache将用于所有持久化类。 我们还可以通过使用 cache 元素来明确定义持久化类。
eternal 如果我们指定eternal =“true”,则不需要定义timeToIdleSeconds和timeToLiveSeconds属性,因为它将由hibernate内部处理。 指定eternal =“false”给程序员控制,但是我们需要定义timeToIdleSeconds和timeToLiveSeconds属性timeToIdleSeconds它定义了二级缓存中对象可以空闲多少秒。
timeToLiveSeconds它定义了在第二级缓存中对象可以存储多少秒,无论它是否空闲

Test.java类如下:
public class Test {    public static void main(String[] args) {        // 在5.1.0版本汇总,hibernate采用如下新方式获取:        // 1. 配置类型安全的准服务注册类,这是当前应用的单例对象,不作修改,所以声明为final        // 在configure("cfg/hibernate.cfg.xml")方法中,如果不指定资源路径,默认在类路径下寻找名为hibernate.cfg.xml的文件        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()                .configure().build();        // 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂        SessionFactory sessionFactory = new MetadataSources(registry)                .buildMetadata().buildSessionFactory();        /**** 上面是配置准备,下面开始我们的数据库操作 ******/        Session session = sessionFactory.openSession();// 从会话工厂获取一个session        // creating transaction object        Transaction transaction = session.beginTransaction();        Statistics stats = sessionFactory.getStatistics();        System.out.println("Stats enabled="+stats.isStatisticsEnabled());        stats.setStatisticsEnabled(true);        System.out.println("Stats enabled="+stats.isStatisticsEnabled());        session.save(new Student(1, "tom"));        session.save(new Student(2, "jary"));        Session session1 = sessionFactory.openSession();        Student student = session1.load(Student.class, 1);        System.out.println(student.getId() + " >>>" + student.getName());        session1.close();        //再次查询ID=1的信息,因为使用了缓存,这里不会再发出查询语句...        Session session11 = sessionFactory.openSession();        Student student1 = session11.load(Student.class, 1);        System.out.println(student1.getId() + " " + student1.getName());        session11.close();        Session session2 = sessionFactory.openSession();        Student student2 = session2.load(Student.class, 2);        System.out.println(student2.getId() + " " + student2.getName());        session2.close();        transaction.commit();        session.close();        sessionFactory.close();        System.out.println("successfully saved");    }}

运行结果如下:

可以看到,hibernate不会发出两次查询。 如果不使用二级缓存,hibernate将会发出两次查询,因为这两个查询都使用不同的会话对象。
参考:
http://www.yiibai.com/hibernate/hibernate-second-level-cache.html
http://blog.csdn.net/runming56/article/details/8574047
http://blog.csdn.net/xlgen157387/article/details/40071651
http://blog.csdn.net/luckyzhoustar/article/details/47748179
http://www.blogjava.net/supercrsky/articles/238580.html