`
semi_sleep
  • 浏览: 102211 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Summary for hibernate cache

阅读更多

You have the option to tell Hibernate which caching implementation to use by specifying the name of a class that

implements org.hibernate.cache.CacheProvider using the property hibernate.cache.provider_class.


<cache
    usage="transactional|read-write|nonstrict-read-write|read-only"  (1)
    region="RegionName"                                              (2)
    include="all|non-lazy"                                           (3)
/>
(1)  usage (required) specifies the caching strategy: transactional, read-write, nonstrict-read-write or read-only
(2)  region (optional, defaults to the class or collection role name) specifies the name of the second level cache

region
(3)  include (optional, defaults to all) non-lazy specifies that properties of the entity mapped with lazy="true" may

not be cached when attribute-level lazy fetching is enabled


Alternatively (preferably?), you may specify <class-cache> and <collection-cache> elements in hibernate.cfg.xml.


The CacheMode controls how a particular session interacts with the second-level cache.
CacheMode.NORMAL - read items from and write items to the second-level cache
CacheMode.GET - read items from the second-level cache, but don't write to the second-level cache except when updating

data
CacheMode.PUT - write items to the second-level cache, but don't read from the second-level cache
CacheMode.REFRESH - write items to the second-level cache, but don't read from the second-level cache, bypass the effect

of hibernate.cache.use_minimal_puts, forcing a refresh of the second-level cache for all items read from the database


To browse the contents of a second-level or query cache region, use the Statistics API:
Map cacheEntries = sessionFactory.getStatistics()
        .getSecondLevelCacheStatistics(regionName)
        .getEntries();


You'll need to enable statistics, and, optionally, force Hibernate to keep the cache entries in a more human-

understandable format:
hibernate.generate_statistics true
hibernate.cache.use_structured_entries true


Query result sets may also be cached. This is only useful for queries that are run frequently with the same parameters.

To use the query cache you must first enable it:
hibernate.cache.use_query_cache true


Note that the query cache does not cache the state of the actual entities in the result set; it caches only identifier

values and results of value type. So the query cache should always be used in conjunction with the second-level cache.


Most queries do not benefit from caching, so by default queries are not cached. To enable caching, call

Query.setCacheable(true). This call allows the query to look for existing cache results or add its results to the cache

when it is executed.


这里还有一个很容易被忽视的重要问题,即打开查询缓存以后,即使是list方法也可能遇到1+N的问题!相同条件第一次list的时候,因

为查询缓存中找不到,不管class缓存是否存在数据,总是发送一条sql语句到数据库获取全部数据,然后填充查询缓存和class缓存。但

是第二次执行的时候,问题就来了,如果你的class缓存的超时时间比较短,现在class缓存都超时了,但是查询缓存还在,那么list方法

在获取id串以后,将会一个一个去数据库load!因此,class缓存的超时时间一定不能短于查询缓存设置的超时时间!如果还设置了发呆

时间的话,保证class缓存的发呆时间也大于查询的缓存的生存时间。这里还有其他情况,比如class缓存被程序强制evict了,这种情况

就请自己注意了。


缓存策略
只读缓存(read-only):没有什么好说的
读/写缓存(read-write):程序可能要的更新数据
不严格的读/写缓存(nonstrict-read-write):需要更新数据,但是两个事务更新同一条记录的可能性很小,性能比读写缓存好
事务缓存(transactional):缓存支持事务,发生异常的时候,缓存也能够回滚,只支持jta环境,这个我没有怎么研究过


读写缓存和不严格读写缓存在实现上的区别在于,读写缓存更新缓存的时候会把缓存里面的数据换成一个锁,其他事务如果去取相应的缓

存数据,发现被锁住了,然后就直接取数据库查询。
在hibernate2.1的ehcache实现中,如果锁住部分缓存的事务发生了异常,那么缓存会一直被锁住,直到60秒后超时。
不严格读写缓存不锁定缓存中的数据。


Collection的缓存和前面查询缓存的list一样,也是只保持一串id,但它不会因为这个表更新过就失效,一个collection缓存仅在这个

collection里面的元素有增删时才失效。

分享到:
评论

相关推荐

    Power Consumption Summary for KeyStone C66x Devices.pdf

    本文档适用于TMS320C66x系列芯片的开发。本文讨论通过设备专用电源电子表格,估算德州仪器的keystone系列数字信号处理器(DSP)使用的功耗问题。设备的功耗高度依赖于应用程序,因此允许根据提供的电源电子表格,用于...

    Summary for XML Schema

    这篇博文的标题“Summary for XML Schema”暗示了我们将探讨XML Schema的基本概念、用途、核心特性以及如何使用它来验证XML文档。 XML是一种标记语言,用于描述数据和其结构,广泛应用于网络数据交换、配置文件和...

    Hibernate+中文文档

    目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate入门 ...20.1. Summary 20.2. SchemaExport命令行选项 20.3. SchemaExport 连接属性 20.4. SchemaUpdate命令行选项 20.5. SchemaValidator命令行参数

    hibernate3.2中文文档(chm格式)

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 ...20.1. Summary 20.2. SchemaExport命令行选项 20.3. SchemaExport 连接属性 20.4. SchemaUpdate命令行选项 20.5. SchemaValidator命令行参数

    HibernateAPI中文版.chm

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 ...20.1. Summary 20.2. SchemaExport命令行选项 20.3. SchemaExport 连接属性 20.4. SchemaUpdate命令行选项 20.5. SchemaValidator命令行参数

    Hibernate 5.3.1api 文档

    10. **Caching策略**:除了第二级缓存,还可以配置查询缓存,通过@Cacheable和@Cache注解控制特定的查询结果缓存。 在提供的压缩包文件中,`stylesheet.css`是样式表文件,用于美化API文档的展示;`index-all.html`...

    Q2 Summary for tp

    supply chain experience

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 ...20.1. Summary 20.2. SchemaExport命令行选项 20.3. SchemaExport 连接属性 20.4. SchemaUpdate命令行选项 20.5. SchemaValidator命令行参数

    关于Oracle的 Clob数据类型在Hibernate中的应用小结

    &lt;column name="SUMMARY_CLOB" /&gt; ``` 4.2 CRUD的实现: 4.2.1 创建(Create): 在保存实体时,需要将String类型的会议内容转换为Clob对象,再设置给实体的summaryClob属性。 4.2.2 更新(Update): 更新时,同样...

    technical summary

    自己写的项目总结(未完成版)想要密码请联系本人

    Hibernate Reference Documentation3.1

    1.5. Summary 2. Architecture 2.1. Overview 2.2. Instance states 2.3. JMX Integration 2.4. JCA Support 2.5. Contextual Sessions 3. Configuration 3.1. Programmatic configuration 3.2. Obtaining a ...

    hibernate说明文档

    The Overview page is the front page of this API document and provides a list of all packages with a summary for each. This page can also contain an overall description of the set of packages.

    DEV控件GridControl的分组和Summary

    ### DEV控件GridControl的分组和Summary知识点详解 #### 一、概述 在使用DevExpress的GridControl组件进行数据展示时,我们经常会遇到需要对数据进行分组或统计的需求。本文将详细介绍如何在GridControl中实现分组...

    MISRA-C-2012-Standards-Model-Summary-for-C.zip_MISRA 2012_MISRA

    MISRA-C:2012 Standards Model Summary for C / C++ Please note that this is only SUMMARY version of the MISRA-C -: 2012.It is correct as of 25th September 2013.

    关于Clob类型在Hibernate中 的应用小结

    &lt;column name="SUMMARY_CLOB" /&gt; ``` **CRUD操作**: 4.2.1 **创建(Create)**: 在创建新记录时,你需要将待存入的文本转换为Clob对象,然后通过SessionFactory的currentSession创建并保存实体。 4.2.2 **...

    TypeScript_BibiGPT v1 oneClick AI Summary for AudioVideo.zip

    typescript

    Oracle 11g New Features for Administrators Summary Sheets by Ahmed baraka

    本文将围绕Ahmed Baraka所编撰的《Oracle 11g New Features for Administrators Summary Sheets》一书中的关键知识点,为读者提供深入理解Oracle 11g新特性的平台。 ### 新概念与定义 Oracle 11g引入了一系列新...

    RFC3842 A Message Summary and Message Waiting Indication Event Package for the Session Initiation Protocol (SIP).docx

    RFC3842 A Message Summary and Message Waiting Indication Event Package for the Session Initiation Protocol (SIP) RFC3842是Session Initiation Protocol(SIP)的一个事件包,用于传输消息等待状态和消息...

    毕业设计-summary for code analysis and autorefactor代码分析与自动化重.zip

    这里为你收集整理了关于毕业设计、课程设计可参考借鉴的资料一份,质量非常高,如果你投入时间去研究几天相信肯定对你有很大的帮助。到时候你会回来感谢我的。 本资源是经过本地编译测试、可打开、可运行的项目、...

    BibiGPT_v1_·_one-Click_AI_Summary_for_AudioVideo__BibiGPT-v1.zip

    BibiGPT_v1_·_one-Click_AI_Summary_for_AudioVideo__BibiGPT-v1

Global site tag (gtag.js) - Google Analytics