初学者易上手的SSH-hibernate03 三大状态与缓存

这章主要来浅的学习下hibernate三大状态与缓存。首先来看下三大状态:

如上图,三大状态分别为临时状态(Transient),持久化状态(Persistent),游离状态(Detached)。那么三大状态具体怎么来解释?

Student stu = new Student("学生1","","长沙",20);

如上代码段,new一个学生对象,内存中有了该对象,但数据库与session缓存中并不存在,即此时为临时状态

        Student stu = new Student("学生1", "", "长沙", 20);
        
        session.saveOrUpdate(stu);

transaction.commit();

 new出来的对象加入到session缓存中,并同过底层的sql语句操作对象到数据库中,即此时为持久化状态。

        session.close();
    
        sessionfactory.close();

 关闭session对象后,学生对象依然存在数据库中,但却已不存在于session中,即脱离了session对象,此时为游离状态。

接下来看缓存:

缓存是介于物理数据源与应用程序之间,是对数据库中的数据复制一份临时放在内存中的容器,其作用是为了减少应用程序对物理数据源访问的次数,从而提高了应用程序的运行性能。Hibernate在进行读取数据的时候,根据缓存机制在相应的缓存中查询,如果在缓存中找到了需要的数据(我们把这称做“缓存命 中"),则就直接把命中的数据作为结果加以利用,避免了大量发送SQL语句到数据库查询的性能损耗。

hibernate缓存分为一级缓存与二级缓存,一级缓存就是我们常用的session,而二级缓存就是SessionFactory。

Session内置不能被卸载,Session的缓存是事务范围的缓存(Session对象的生命周期通常对应一个数据库事务或者一个应用事务)。
一级缓存中,持久化类的每个实例都具有唯一的OID。

由于SessionFactory对象的生命周期和应用程序的整个过程对应,因此Hibernate二级缓存是进程范围或者集群范围的缓存,有可能出现并发问题,因此需要采用适当的并发访问策略,该策略为被缓存的数据提供了事务隔离级别。
第二级缓存是可选的,是一个可配置的插件,默认下SessionFactory不会启用这个插件。
Hibernate提供了org.hibernate.cache.CacheProvider接口,它充当缓存插件与Hibernate之间的适配器。

在通常情况下会将具有以下特征的数据放入到二级缓存中:

●   很少被修改的数据。

●   不是很重要的数据,允许出现偶尔并发的数据。

●   不会被并发访问的数据。

●   参考数据。

       而对于具有以下特征的数据则不适合放在二级缓存中:

●   经常被修改的数据。

●   财务数据,绝对不允许出现并发。

●   与其他应用共享的数据。

那么来配置一个二级缓存吧。

首先要导入ehcache包以及hibernate-ehcache包(对应hibernate版本),既然是maven项目,那么当然在pom.xml中引入依赖。

<!--hibernate-ehcache -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.2.10.Final</version>
        </dependency>

        <!--net.sf.ehcache/ehcache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.4</version>
        </dependency>

接下来在hibernate.cfg.xml中进行配置

        <!-- 开启二级缓存 -->
        <property name="cache.use_second_level_cache">true</property>
        <!-- 开启查询二级缓存 -->
        <property name="cache.use_query_cache"></property>
        <!-- Hibernate4.0以上设置factory 缓存工具 -->
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <!-- 二级缓存 ehcache的配置文件位置 -->
        <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>
        <!-- 映射文件 -->
        <mapping resource="com/entity/Student.hbm.xml" />
        <!-- 需要缓存的类 -->
        <class-cache usage="read-only" class="com.entity.Student" />

其中的ehcache.xml文件放与hibernate.cfg.xml同级即可。ehcache.xml里面的元素可以直接在以下路径中找到,不需要自己写。项目->Java Resources->Libraries->Maven Dependencies->ehcache-2.10.4.jar->ehcache-failsafe.xml

找到后打开,全选copy复制,再把<diskStore path="java.io.tmpdir"/>节点中path路径改成你自己定义的即可。最后测试二级缓存。

@Test
    public void test() {
        // 获取配置信息
        Configuration configuration = new Configuration().configure();
        // 得到Session工厂
        SessionFactory factory = configuration.buildSessionFactory();
        // 得到Session
        Session session = factory.openSession();
        Session session1 = factory.openSession();
        // 开启事物
        Transaction transaction = session.beginTransaction();
        // 根据主键得到对象(查询单个记录)
        Student st = session.get(Student.class, 1);
        System.out.println(st);
        // 二级缓存
        List<Student> ls = session1.createQuery("from Student").setCacheable(true).list();
        for (Student s : ls) {
            System.out.println(s);
        }
        // 提交事物
        // transaction.commit();
        // 关闭seeion
        session.close();
        // 关闭SessionFactory
        factory.close();
    }
自己对缓存也不是很懂!
本章结束。
原文地址:https://www.cnblogs.com/lzx2509254166/p/7743632.html