Hibernate二級快取

2021-07-25 11:20:44 字數 3409 閱讀 1498

hibernate的session在事務級別進行持久化資料的快取操作。 當然,也有可能分別為每個類(或集合),配置集群、或jvm級別(sessionfactory級別)的快取。 你甚至可以為之插入乙個集群的快取。注意,快取永遠不知道其他應用程式對持久化倉庫(資料庫)可能進行的修改 (即使可以將快取資料設定為定期失效)。

通過在hibernate.cache.provider_class屬性中指定org.hibernate.cache.cacheprovider的某個實現的類名,你可以選擇讓hibernate使用哪個快取實現。hibernate打包一些開源快取實現,提供對它們的內建支援(見下表)。除此之外,你也可以實現你自己的實現,將它們插入到系統中。注意,在3.2版本之前,預設使用ehcache 作為快取實現,但從3.2起就不再這樣了。

1.  快取策略提供商(cache providers)

cache

provider class

type

cluster safe

query cache supported

hashtable (not intended for production use)

org.hibernate.cache.hashtablecacheprovidermemory

yesehcache

org.hibernate.cache.ehcacheprovidermemory, disk

yesoscache

org.hibernate.cache.oscacheprovidermemory, disk

yesswarmcache

org.hibernate.cache.swarmcacheproviderclustered (ip multicast)

yes (clustered invalidation)

jboss treecache

org.hibernate.cache.treecacheproviderclustered (ip multicast), transactional

yes (replication)

yes (clock sync req.)

類或者集合對映的「元素」可以有下列形式:

usage(必須)說明了快取的策略:transactionalread-writenonstrict-read-writeread-only

region(可選, 預設為類或者集合的名字(class or collection role name)) 指定第二級快取的區域名(name of the second level cache region)

include(可選,預設為all)non-lazy當屬性級延遲抓取開啟時, 標記為lazy="true"的實體的屬性可能無法被快取

另外(首選?), 你可以在hibernate.cfg.xml中指定元素。

這裡的usage屬性指明了快取併發策略(cache concurrency strategy)

3. 策略:唯讀快取(strategy: read only)

如果你的應用程式只需讀取乙個持久化類的例項,而無需對其修改, 那麼就可以對其進行唯讀快取。這是最簡單,也是實用性最好的方法。甚至在集群中,它也能完美地運作。

....

4.策略:讀/寫快取(strategy: read/write)
如果應用程式需要更新資料,那麼使用讀/寫快取比較合適。 如果應用程式要求「序列化事務」的隔離級別(serializable transaction isolation level),那麼就決不能使用這種快取策略。 如果在jta環境中使用快取,你必須指定hibernate.transaction.manager_lookup_class屬性的值, 通過它,hibernate才能知道該應用程式中jta的transactionmanager的具體策略。 在其它環境中,你必須保證在session.close()、或session.disconnect()呼叫前, 整個事務已經結束。 如果你想在集群環境中使用此策略,你必須保證底層的快取實現支援鎖定(locking)。hibernate內建的快取策略並不支援鎖定功能。

....

....

5.策略:非嚴格讀/寫快取(strategy: nonstrict read/write)
如果應用程式只偶爾需要更新資料(也就是說,兩個事務同時更新同一記錄的情況很不常見),也不需要十分嚴格的事務隔離, 那麼比較適合使用非嚴格讀/寫快取策略。如果在jta環境中使用該策略, 你必須為其指定hibernate.transaction.manager_lookup_class屬性的值, 在其它環境中,你必須保證在session.close()、或session.disconnect()呼叫前, 整個事務已經結束。

6.  策略:事務快取(transactional)

hibernate的事務快取策略提供了全事務的快取支援, 例如對jboss treecache的支援。這樣的快取只能用於jta環境中,你必須指定 為其hibernate.transaction.manager_lookup_class屬性。

沒有一種快取提供商能夠支援上列的所有快取併發策略。下表中列出了各種提供器、及其各自適用的併發策略。

7.各種快取提供商對快取併發策略的支援情況(cache concurrency strategy support)

cache

read-only

nonstrict-read-write

read-write

transactional

hashtable (not intended for production use)

yesyes

yesehcache

yesyes

yesoscache

yesyes

yesswarmcache

yesyes

jboss treecache

yesyes

hibernate二級快取

cacheconcurrencystrategy.none cacheconcurrencystrategy.read only 唯讀模式,在此模式下,如果對資料進行更新操作,會有異常 cacheconcurrencystrategy.read write 讀寫模式在更新快取的時候會把快取裡面的資料...

hibernate 二級快取

session快取 一級快取 sql查詢結果快取,由hibernate管理 sessionfactory內建快取,內建快取是hibernate自帶的,用於存放預定義的sql以及hbm.xml描述的元資料,不可解除安裝 sessionfactory外接快取 二級快取 由外部外掛程式提供,外接快取的資料...

Hibernate二級快取

hibernate提供的快取 有一級快取 二級快取。目的是為了減少對資料庫的訪問次數,提公升程式執行效率!一級快取 基於session 的快取,快取內容只在當前 session 有效,session 關閉,快取內容失效!特點 作用範圍較小!快取的事件短。快取效果不明顯。二級快取 hibernate提...