美文网首页
hibernate 缓存及对象的三种状态

hibernate 缓存及对象的三种状态

作者: bigpeng个人博客 | 来源:发表于2018-07-31 15:39 被阅读8次

    1、hibernate缓存机制
    hibernate中使用缓存,大大提高了数据查询的效率,不需要每次都去数据库去读取。其缓存又分为两种缓存。
    1)一级缓存:是session级别的缓存,session关闭之后,缓存的内容就没了。(默认开启),需要手动配置开启,session关闭之后
    2)二级缓存:是sessionFactory级别的缓存,需要第三方缓存包支持,默认不开启,需要手动配置。关闭session之后缓存还会存在,关闭SessionFactory之后二级缓存清除。
    具体配置如下:
    a)导入缓存的jar包
    b)在hibernate.cfg.xml中配置开启二级缓存

    <!-- 开启二级缓存 -->
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <!-- 二级缓存的提供类 在hibernate4.0版本以后我们都是配置这个属性来指定二级缓存的提供类-->
     <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>  
    <!-- 开启查询缓存 -->
    <property name="hibernate.cache.use_query_cache">true</property>
    

    c)创建缓存配置文件ehcache.xml

    <ehcache>
    
        <!-- Sets the path to the directory where cache .data files are created.
    
             If the path is a Java System Property it is replaced by
             its value in the running VM.
    
             The following properties are translated:
             user.home - User's home directory
             user.dir - User's current working directory
             java.io.tmpdir - Default temp file path -->
      
      <!--指定二级缓存存放在磁盘上的位置-->
        <diskStore path="user.dir"/>  
    
      <!--我们可以给每个实体类指定一个对应的缓存,如果没有匹配到该类,则使用这个默认的缓存配置-->
        <defaultCache
            maxElementsInMemory="10000"  //在内存中存放的最大对象数
            eternal="false"         //是否永久保存缓存,设置成false
            timeToIdleSeconds="120"    
            timeToLiveSeconds="120"    
            overflowToDisk="true"     //如果对象数量超过内存中最大的数,是否将其保存到磁盘中,设置成true
            />
      
      <!--
        1、timeToLiveSeconds的定义是:以创建时间为基准开始计算的超时时长;
        2、timeToIdleSeconds的定义是:在创建时间和最近访问时间中取出离现在最近的时间作为基准计算的超时时长;
        3、如果仅设置了timeToLiveSeconds,则该对象的超时时间=创建时间+timeToLiveSeconds,假设为A;
        4、如果没设置timeToLiveSeconds,则该对象的超时时间=max(创建时间,最近访问时间)+timeToIdleSeconds,假设为B;
        5、如果两者都设置了,则取出A、B最少的值,即min(A,B),表示只要有一个超时成立即算超时。
      -->
    
      <!--可以给每个实体类指定一个配置文件,通过name属性指定,要使用类的全名-->
        <cache name="com.xiaoluo.bean.Student"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="600"
            overflowToDisk="true"
            />
    
        <cache name="sampleCache2"
            maxElementsInMemory="1000"
            eternal="true"
            timeToIdleSeconds="0"
            timeToLiveSeconds="0"
            overflowToDisk="false"
            /> -->
    
    
    </ehcache>
    

    d)在对应的实体类中添加配置,开启二级缓存

    <hibernate-mapping package="com.xiaoluo.bean">
        <class name="Student" table="t_student">
            <!-- 二级缓存一般设置为只读的 -->
            <cache usage="read-only"/>
            <id name="id" type="int" column="id">
                <generator class="native"/>
            </id>
            <property name="name" column="name" type="string"></property>
            <property name="sex" column="sex" type="string"></property>
            <many-to-one name="room" column="rid" fetch="join"></many-to-one>
        </class>
    </hibernate-mapping>
    

    2、hibernate中对象有三种状态,其分别为
    1)瞬时状态:对象刚刚创建,还没有使用session对该对象做任何操作,缓存中不存在该对象。那么这个对象为瞬时状态。(数据库中没有,session缓存中也没有)
    2)持久状态:当调用session的save()或者saveOrUpdate()方法时,对象保存进入数据库,之后我们称该对象为持久状态;(数据库中有,session缓存中没有)
    3)游离状态:持久化的对象调用close(),clear()等方法,将session中的该对象清除了,那么此时该对象处理游离态。(数据库中有,session缓存中没有)

    参考资料:
    1、对象状态:https://www.cnblogs.com/xiaoluo501395377/p/3380270.html
    2、缓存机制:https://www.cnblogs.com/xiaoluo501395377/p/3377604.html

    相关文章

      网友评论

          本文标题:hibernate 缓存及对象的三种状态

          本文链接:https://www.haomeiwen.com/subject/avpomftx.html