四、持久层框架(Hibernate)

一、一级缓存与二级缓存

  1、一级缓存:Hibernate默认是开启一级缓存的,一级缓存存放在session里,一个Session做一次查询操作,会把这个操作的结果放在一级缓存中,如果短时间内这个session (同一个session)又做了同一个操作,那么hibernate直接从一级缓存中拿,而不用再去连接数据库,取数据。

一级缓存生命周期依赖于Session生命周期,Session被关闭后,缓存也结束。

  2、二级缓存:就是SessionFactory级别的缓存,查询的结果缓存到二级缓存中,同一个sessionFactory创建的某个session执行相同的操作,hibernate会从二级缓存中取结果,不会重新连接数据库

二级缓存被同一个SessionFactory里的所有Session共享,Session进行并发访问缓存时,必须对缓存更新,缓存生命周期依赖于SessionFactory周期,当SessionFactory被关闭后,二级缓存也结束。

二、一级缓存的应用代码如下 

Hibernate的一级缓存在Session上

package com.demo.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.demo.pojo.Category;

public class TetHibernate{
    public static void main(String[] args){
        SessionFactory sf=new Configuration().configure.buildSessionFactory();//建立一个SessionFactory对象
        Session session=sf.openSession();//开启一个Session
        session.beginTransaction();//开启事务
        
        Category c1=(Category)session.get(Category.class,1);
        System.out.println(c1.getName());
        Category c2=(Category)session.get(Category.class,1);
        System.out.println(c2.getName());
        
        session.getTransaction().commit();
        session.close();
        sf.close();
    }
}
View Code

三、二级缓存的应用代码如下

Hibernate的二级缓存在SessionFactory上

创建两个Session,session1,session2

session1:第一次获取id=1的Category会执行SQL语句,第二次获取id=1的Category不会执行SQL语句,因为有一级缓存

session2:获取id=1的Category,会执行SQL语句,因为第二个Session,没有缓存这个,所以会出现两条SQL语句执行。

package com.demo.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.demo.pojo.Category;

public class TetHibernate{
    public static void main(String[] args){
        SessionFactory sf=new Configuration().configure.buildSessionFactory();//建立一个SessionFactory对象
        
        Session session1=sf.openSession();//开启ession1
        session1.beginTransaction();//开启session1事务        
        Category c1=(Category)session1.get(Category.class,1);
        Category c2=(Category)session1.get(Category.class,1);
        
        session1.getTransaction().commit();
        session1.close();
        
        Session session2=sf.openSession()//开启session2
        session2.beginTransaction();
        Category c3=(Category)session2.get(Category.class,1);
        session2.getTransaction().commit();
        session2.close();
        
        sf.close();
    }
}
View Code

四、hibernate.cfg.xml中增加对二级缓存的配置

hibernate本身不提供二级缓存,都是使用第三方二级缓存插件,我们使用EhCache提供的二级缓存。

<?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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=GBK</property>
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        
        <!--使用第三方插件Ehchache提供的二级缓存-->
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
            
        <mapping resource="com/demo/pojo/Product.hbm.xml" />
        <mapping resource="com/demo/pojo/Category.hbm.xml" />
        <mapping resource="com/demo/pojo/User.hbm.xml" />
        
    </session-factory>

</hibernate-configuration>
View Code

五、在src目录下,创建一个ehcache.xml用来对EhCache的缓存配置

<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
</ehcache>

六、对于要进行二级缓存的实体类的映射文件,进行相应配置

  增加<cache usage="read-only"/>,比如对Category.hbm.xml进行二级缓存

<?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.demo.pojo">
    <class name="Category" table="category_table">
        <!--设置二级缓存配置-->
        <cache usage="read-only" />
        
        <id name="id" column="id">
            <generator class="native">
            </generator>
        </id>
        <property name="name" />
        <!--set用来设置一对多,或者多对多-->
        <set name="products" lazy="true">
            <key column="cid" not-null="false" />
            <one-to-many class="Product" />
        </set>    
                
    </class>    
</hibernate-mapping>
View Code

七、测试二级缓存 

package com.demo.test;
  
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
import com.demo.pojo.Category;
  
public class TestHibernate {
    public static void main(String[] args) {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        
        Session session1 = sf.openSession();//建立session1
        session1.beginTransaction();//开启session1事务       
        Category c1 = (Category)session1.get(Category.class, 1);//第一次在连接数据库后获取到数据
        Category c2= (Category)session1.get(Category.class, 1);//不用再次执行sql连接数据库,直接从一级缓存从取
        session1.getTransaction().commit();
        session1.close();
        
        Session session2 = sf.openSession();//建立session2
        session2.beginTransaction();//开启session2事务
        Category c3 = (Category)session2.get(Category.class, 1);//由于给Category设置二级缓存,所以二级缓存在同一个SessionFactory下,不同session之间,可以共享,不用再次连接数据库,直接在SessionFactory中其获取数据
        session2.getTransaction().commit();
        session2.close();
        
        sf.close();
    }
}
View Code
原文地址:https://www.cnblogs.com/drq1/p/8515254.html