zoukankan      html  css  js  c++  java
  • Hibernate缓存

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

     

    Hibernate缓存分类:

    一、Session缓存(又称作事务缓存):Hibernate内置的,不能卸除。

    缓存范围:缓存只能被当前Session对象访问。缓存的生命周期依赖于Session的生命周期,当Session被关闭后,缓存也就结束生命周期。

    二、SessionFactory缓存(又称作应用缓存):使用第三方插件,可插拔。

    缓存范围:缓存被应用范围内的所有session共享。这些session有可能是并发访问缓存,因此必须对缓存进行更新。缓存的生命周期依赖于应用的生命周期,应用结束时,缓存也就结束了生命周期,二级缓存存在于应用程序范围。

     

    Hibernate一些与一级缓存相关的操作(时间点):

    数据放入缓存:

    1. save()。当session对象调用save()方法保存一个对象后,该对象会被放入到session的缓存中。

    2. get()和load()。当session对象调用get()或load()方法从数据库取出一个对象后,该对象也会被放入到session的缓存中。

    3. 使用HQL和QBC等从数据库中查询数据。

    例如:数据库有一张表如下:

    使用get()或load()证明缓存的存在:

    复制代码
    public class Client
    {
    public static void main(String[] args)
    {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;
    try
    {
    /*开启一个事务*/
    tx = session.beginTransaction();
    /*从数据库中获取id="402881e534fa5a440134fa5a45340002"的Customer对象*/
    Customer customer1 = (Customer)session.get(Customer.class, "402881e534fa5a440134fa5a45340002");
    System.out.println("customer.getUsername is"+customer1.getUsername());
    /*事务提交*/
    tx.commit();

    System.out.println("-------------------------------------");

    /*开启一个新事务*/
    tx = session.beginTransaction();
    /*从数据库中获取id="402881e534fa5a440134fa5a45340002"的Customer对象*/
    Customer customer2 = (Customer)session.get(Customer.class, "402881e534fa5a440134fa5a45340002");
    System.out.println("customer2.getUsername is"+customer2.getUsername());
    /*事务提交*/
    tx.commit();

    System.out.println("-------------------------------------");

    /*比较两个get()方法获取的对象是否是同一个对象*/
    System.out.println("customer1 == customer2 result is "+(customer1==customer2));
    }
    catch (Exception e)
    {
    if(tx!=null)
    {
    tx.rollback();
    }
    }
    finally
    {
    session.close();
    }
    }
    }
    复制代码

    程序控制台输出结果:

    复制代码
    Hibernate: 
    select
    customer0_.id as id0_0_,
    customer0_.username as username0_0_,
    customer0_.balance as balance0_0_
    from
    customer customer0_
    where
    customer0_.id=?
    customer.getUsername islisi
    -------------------------------------
    customer2.getUsername islisi
    -------------------------------------
    customer1 == customer2 result is true
    复制代码

      输出结果中只包含了一条SELECT SQL语句,而且customer1 == customer2 result is true说明两个取出来的对象是同一个对象。其原理是:第一次调用get()方法, Hibernate先检索缓存中是否有该查找对象,发现没有,Hibernate发送SELECT语句到数据库中取出相应的对象,然后将该对象放入缓存中,以便下次使用,第二次调用get()方法,Hibernate先检索缓存中是否有该查找对象,发现正好有该查找对象,就从缓存中取出来,不再去数据库中检索。
    数据从缓存中清除:

    1. evit()将指定的持久化对象从缓存中清除,释放对象所占用的内存资源,指定对象从持久化状态变为脱管状态,从而成为游离对象。 
    2. clear()将缓存中的所有持久化对象清除,释放其占用的内存资源。

    其他缓存操作:

    1. contains()判断指定的对象是否存在于缓存中。
    2. flush()刷新缓存区的内容,使之与数据库数据保持同步。

     


    Hibernate使用二级缓存

    适合存放到第二级缓存中的数据: 
    1. 很少被修改的数据。 
    2. 不是很重要的数据,允许出现偶尔并发的数据。 
    3. 不会被并发访问的数据。 
    4. 参考数据,指的是供应用参考的常量数据,它的实例数目有限,它的实例会被许多其他类的实例引用,实例极少或者从来不会被修改。 
    不适合存放到第二级缓存的数据:
    1. 经常被修改的数据。
    2. 财务数据,绝对不允许出现并发。
    3. 与其他应用共享的数据。

      Hibernate如何将数据库中的数据放入到二级缓存中?注意,你可以把缓存看做是一个Map对象,它的Key用于存储对象OID,Value用于存储POJO。首先,当我们使用Hibernate从数据库中查询出数据,获取检索的数据后,Hibernate将检索出来的对象的OID放入缓存中key中,然后将具体的POJO放入value中,等待下一次再次向数据查询数据时,Hibernate根据你提供的OID先检索一级缓存,若没有且配置了二级缓存,则检索二级缓存,如果还没有则才向数据库发送SQL语句,然后将查询出来的对象放入缓存中。

    为Hibernate配置二级缓存:

    在主配置文件中hibernate.cfg.xml 

        <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

    配置ehcache.xml

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache>
    <!--
    缓存到硬盘的路径
    -->
    <diskStore path="d:/ehcache"></diskStore>

    <!--
    默认设置
    maxElementsInMemory : 在內存中最大緩存的对象数量。
    eternal : 缓存的对象是否永远不变。
    timeToIdleSeconds :可以操作对象的时间。
    timeToLiveSeconds :缓存中对象的生命周期,时间到后查询数据会从数据库中读取。
    overflowToDisk :内存满了,是否要缓存到硬盘。
    -->
    <defaultCache maxElementsInMemory="200" eternal="false"
    timeToIdleSeconds
    ="50" timeToLiveSeconds="60" overflowToDisk="true"></defaultCache>

    <!--
    指定缓存的对象。
    下面出现的的属性覆盖上面出现的,没出现的继承上面的。
    -->
    <cache name="com.suxiaolei.hibernate.pojos.Order" maxElementsInMemory="200" eternal="false"
    timeToIdleSeconds
    ="50" timeToLiveSeconds="60" overflowToDisk="true"></cache>

    </ehcache>
    复制代码

    在需要被缓存的对象中hbm文件中的<class>标签下添加一个<cache>子标签

    复制代码
        <hibernate-mapping>
    <class name="com.suxiaolei.hibernate.pojos.Order" table="orders">
    <cache usage="read-only"/>

    <id name="id" type="string">
    <column name="id"></column>
    <generator class="uuid"></generator>
    </id>

    <property name="orderNumber" column="orderNumber" type="string"></property>
    <property name="cost" column="cost" type="integer"></property>

    <many-to-one name="customer" class="com.suxiaolei.hibernate.pojos.Customer"
    column
    ="customer_id" cascade="save-update">
    </many-to-one>
    </class>
    </hibernate-mapping>
    复制代码

      若存在一对多的关系,想要在在获取一方的时候将关联的多方缓存起来,需要再集合属性下添加<cache>子标签,这里需要将关联的对象的hbm文件中必须在存在<class>标签下也添加<cache>标签,不然Hibernate只会缓存OID。

     

    复制代码
        <hibernate-mapping>
    <class name="com.suxiaolei.hibernate.pojos.Customer" table="customer">
    <!-- 主键设置 -->
    <id name="id" type="string">
    <column name="id"></column>
    <generator class="uuid"></generator>
    </id>

    <!-- 属性设置 -->
    <property name="username" column="username" type="string"></property>
    <property name="balance" column="balance" type="integer"></property>

    <set name="orders" inverse="true" cascade="all" lazy="false" fetch="join">
    <cache usage="read-only"/>
    <key column="customer_id" ></key>
    <one-to-many class="com.suxiaolei.hibernate.pojos.Order"/>
    </set>

    </class>
    </hibernate-mapping>
    复制代码
  • 相关阅读:
    配置postgres9.3间的fdw——实现不同postgres数据库间的互访问
    linux安装配置postgres及使用dblink
    一次“峰回路转”的troubleshooting经历
    10分钟内把永远跑不完的存储过程变为2秒跑完
    C++ friend关键字
    每天学点Linux命令之 vi 命令
    Shell
    九大排序算法及其实现- 插入.冒泡.选择.归并.快速.堆排序.计数.基数.桶排序.堆排序
    到位
    【LeetCode】-- 260. Single Number III
  • 原文地址:https://www.cnblogs.com/gxpblogs/p/3086015.html
Copyright © 2011-2022 走看看