日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

初识Hibernate 缓存

發布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初识Hibernate 缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

    生活就像一杯咖啡,讓你我慢慢的品嘗,品嘗它的苦澀和甘甜......

一、什么是Hibernate緩存。

   解析:白話來說就是緩存數據的容器

      官方標準點緩存:是計算機領域的概念,它介于應用程序和永久性數據存儲源之間。

      作用:降低應用程序直接讀寫數據庫的頻率,從而提高程序的運行性能。緩存中的數據是數據存儲源中數據的拷貝。緩存的物理介質通常是內存。

二、緩存一般分為三個類

  一級緩存

  二級緩存

  查看緩存

三、一級緩存

場景一:使用同一個session連續查詢兩次同一個對象

/查詢學生信息public static void select(){//由班級查詢該班級學生信息Session session=HibernateUtil.currentSession();Grade grade=(Grade) session.get(Grade.class, 14);//輸出班級信息System.out.println(grade.getGname());Grade grade2=(Grade) session.get(Grade.class, 14);//輸出班級信息System.out.println(grade2.getGname());}

執行結果:

場景一:在第一次查詢完畢后,關閉session對象,重新開啟一個session然后繼續查詢同一個對象

//查詢學生信息public static void select(){//由班級查詢該班級學生信息Session session=HibernateUtil.currentSession();Grade grade=(Grade) session.get(Grade.class, 14);//輸出班級信息System.out.println(grade.getGname());//關閉session HibernateUtil.closeSession();//重新獲取sessionsession=HibernateUtil.currentSession();Grade grade2=(Grade) session.get(Grade.class, 14);//輸出班級信息System.out.println(grade2.getGname());}

執行結果:

?

解析:

  ?1:當我沒有關閉session時用的同一個session兩次訪問同一個對象時,只會向DB端發送一條sql語句
    * 原因:因為我第一次訪問數據庫的時候Hibernate會自動的將我查詢出來的結果保留一份查詢出來的對象到一級緩存
       ? ?并且這個額對象是根據OID唯一標識的,也可以理解為數據庫中的主鍵值,然后當我再一次訪問一個對象時,Hibernate
        機制會自動的先去一級緩存中查找看有沒有OID與我要查詢的OID相同的對象,如果有的話,則直接從一級緩存中 拿數據
       ? ?如果沒有相同的OID則說明緩存中沒有我要的記錄,那么就會直接去訪問DB端了,這樣的話,又會重新發送一條sql
  ??2:當我第一次查詢完數據后立即關閉session,這時重新開啟一個session來訪問同一個對象,這時我們會發現它居然向數據庫發送了兩條Sql語句。這是為什么呢?
    * 原因:其實原因很簡單,因為我們雖然說是訪問的同一個對象,但是我們隨即就關閉了這個session而重新開啟了一個session,

        此時我們訪問時的session是不一致的也就是說是兩個不同的session發出的請求,這樣理解的話,我們就不難理解了。

        ??所以總結出,一級緩存是一個會話級別的緩存,當一次回話結束后該會話里的緩存則會全部的銷毀,所有我們自然就只能重新發送一條sql啦。

3補充以下方法支持一級緩存

?

?

?四、二級緩存

(一)配置二級緩存 

  1.引入如下jar包。

?????   ehcache-1.2.3.jar? 核心庫

?????   backport-util-concurrent.jar

?????   commons-logging.jar

  2.配置Hibernate.cfg.xml開啟二級緩存

??     <property name="hibernate.cache.use_second_level_cache">true</property>

?

?

?

? ? ?3.配置二級緩存的供應商

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

?

?

?

  4.指定使用二級緩存的類

    方案一:在*.hbm.xml中配置

??????   在<class元素的子元素下添加chche子節點,但該配置僅會緩存對象的簡單屬性,若希望緩存集合屬性中的元素,必須在set元素中添加<cache>子元素

?????     <class name="Student" table="STUDENT">

        <cache usage="read-write"/>

?

?

?

    方案二:在大配置文件(hibernate.cfg.xml)中配置

        ?<class-cache usage="read-write" class="cn.happy.entity.Student"/>

          <collection-cache usage="read-write" collection=""/>--可注釋如果配置不成功

?

?

?

    注意大配置的書寫位置

      Multiple annotations found at this line:

????     - The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-

????     ?cache)*,event*,listener*)".

?      ? - Start tag of element <session-factory>

?

    *5.在src下添加ehcache.xml文件,從etc獲取文件即可。

      

<ehcache><!-- Sets the path to the directory where cache .data files are created.If the path is a Java System Property it is replaced byits value in the running VM.The following properties are translated:user.home - User's home directoryuser.dir - User's current working directoryjava.io.tmpdir - Default temp file path --><diskStore path="d:/tmp"/><!--Default Cache configuration. These will applied to caches programmatically created throughthe CacheManager.The following attributes are required for defaultCache:maxInMemory - Sets the maximum number of objects that will be created in memoryeternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the elementis never expired.timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only usedif the element is not eternal. Idle time is now - last accessed timetimeToLiveSeconds - Sets the time to live for an element before it expires. Is only usedif the element is not eternal. TTL is now - creation timeoverflowToDisk - Sets whether elements can overflow to disk when the in-memory cachehas reached the maxInMemory limit.--><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="true"/><!--Predefined caches. Add your cache configuration settings here.If you do not have a configuration for your cache a WARNING will be issued when theCacheManager startsThe following attributes are required for defaultCache:name - Sets the name of the cache. This is used to identify the cache. It must be unique.maxInMemory - Sets the maximum number of objects that will be created in memoryeternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the elementis never expired.timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only usedif the element is not eternal. Idle time is now - last accessed timetimeToLiveSeconds - Sets the time to live for an element before it expires. Is only usedif the element is not eternal. TTL is now - creation timeoverflowToDisk - Sets whether elements can overflow to disk when the in-memory cachehas reached the maxInMemory limit.--><!-- Sample cache named sampleCache1This cache contains a maximum in memory of 10000 elements, and will expirean element if it is idle for more than 5 minutes and lives for more than10 minutes.If there are more than 10000 elements it will overflow to thedisk cache, which in this configuration will go to wherever java.io.tmp isdefined on your system. On a standard Linux system this will be /tmp" --><cache name="sampleCache1"maxElementsInMemory="10000"eternal="false"timeToIdleSeconds="300"timeToLiveSeconds="600"overflowToDisk="true"/><!-- Sample cache named sampleCache2This cache contains 1000 elements. Elements will always be held in memory.They are not expired. --><cache name="sampleCache2"maxElementsInMemory="1000"eternal="true"timeToIdleSeconds="0"timeToLiveSeconds="0"overflowToDisk="false"/> --><!-- Place configuration for your caches following --></ehcache>

?

(二)二級緩存散裝數據原理圖?

解析:每次從二級緩存中取出的對象,都是一個新的對象。

(三)什么樣的數據適合放入二級緩存中?

  1很少被修改的數據

  2不是很重要的數據,允許出現偶爾并發的數據

  3不會被并發訪問的數據

  4參考數據,指的是供應用參考的常量數據,它的實列數目有限,它的實列被許多其他類的實列引用,實列極少或者從來不會被修改。

?

?

講述幾乎結束了!!!

?

轉載于:https://www.cnblogs.com/yejiaojiao/p/5798448.html

總結

以上是生活随笔為你收集整理的初识Hibernate 缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。