大量数据流动是web应用性能问题常见的原因,而缓存被广泛的用于优化数据库应用。cache被设计为通过保存从数据库里load的数据来减少应用和数据库之间的数据流动。数据库访问只有当检索的数据不在cache里可用时才必要。hibernate可以用两种不同的对象缓存:first-level cache 和 second-level cache。first-level cache和Session对象关联,而second-level cache是和Session Factory对象关联。
缺省地,hibernate已经使用基于每个事务的first-level cache。Hibernate用first-level cache主要是减少在一个事务内的sql查询数量。例如,如果一个对象在同一个事务内被修改多次,hibernate将只生成一个包括所有修改的UPDATE SQL语句。为了减少数据流动,second-level cache在Session Factory级的不同事务之间保持load的对象,这些对象对整个应用可用,不只是对当前用户正在运行的查询。这样,每次查询将返回已经load在缓存里的对象,避免一个或更多潜在的数据库事务。
下载ehcache,hibernate3.2必须要ehcache1.2以上才能支持。可以修改log4j配置文件log4j.logger.net.sf.hibernate.cache=debug查看日志
1.在类路径上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="java.io.tmpdir"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required:
maxElementsInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the
element is never expired.
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
The following attributes are optional:
timeToIdleSeconds - Sets the time to idle for an element before it expires.
i.e. The maximum amount of time between accesses before an element expires
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that an Element can idle for infinity.
The default value is 0.
timeToLiveSeconds - Sets the time to live for an element before it expires.
i.e. The maximum time between creation time and when an element expires.
Is only used if the element is not eternal.
Optional attribute. A value of 0 means that and Element can live for infinity.
The default value is 0.
diskPersistent - Whether the disk store persists between restarts of the Virtual Machine.
The default value is false.
diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value
is 120 seconds.
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
<!-- See http://ehcache.sourceforge.net/documentation/#mozTocId258426 for how to configure caching for your objects -->
</ehcache>
2.applicationContext-hibernate.xml里Hibernate SessionFactory配置:
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property>
<!-- The property below is commented out b/c it doesn't work when run via
Ant in Eclipse. It works fine for individual JUnit tests and in IDEA ??
<property name="mappingJarLocations">
<list><value>file:dist/appfuse-dao.jar</value></list>
</property>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">@HIBERNATE-DIALECT@</prop>
<!--<prop key="hibernate.show_sql">true</prop>-->
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.hibernate.use_outer_join">true</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!--
<prop key="hibernate.use_sql_comments">false</prop>
-->
<!-- Create/update the database tables automatically when the JVM starts up
<prop key="hibernate.hbm2ddl.auto">update</prop> -->
<!-- Turn batching off for better error messages under PostgreSQL
<prop key="hibernate.jdbc.batch_size">0</prop> -->
</props>
</property>
<property name="entityInterceptor">
<ref local="auditLogInterceptor"/>
</property>
</bean>
说明:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行
3.model类里采用Xdoclet生成*.hbm.xml里的cache xml标签,即<cache usage="read-only"/>
/**
* @hibernate.class table="WF_WORKITEM_HIS"
* @hibernate.cache usage="read-write"
*
*/
4.对于"query cache",需要在程序里编码:
getHibernateTemplate().setCacheQueries(true);
return getHibernateTemplate().find(hql);
分享到:
相关推荐
" Spring+Hibernate 使用 Ehcache 作为 Second-Level Cache" Spring 和 Hibernate 是 Java Web 应用程序开发中两个非常重要的技术栈。Spring 是一个轻量级的控制反转(IoC)容器,提供了一个框架来管理 Java 对象...
在Spring和Hibernate集成的开发环境中,使用EhCache作为缓存机制是常见的优化策略,它能够显著提升应用程序的性能和响应速度。EhCache是一款开源的、高性能的、内存级的分布式缓存解决方案,适用于Java应用程序。...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 接下来,我们要为想要缓存的实体...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 这里我们指定了EhCache作为二...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 3. **实体类配置**:在实体类上...
通过以上步骤,我们就成功地在Spring Boot 2.1.4.RELEASE项目中配置了使用Redis作为Hibernate二级缓存的环境。这将显著提升数据库查询效率,减少对数据库的压力,尤其在高并发场景下,效果尤为明显。记得在实际生产...
2. 打开二级缓存:spring.jpa.properties.hibernate.cache.use_second_level_cache=true 3. 打开查询缓存:spring.jpa.properties.hibernate.cache.use_query_cache=true 4. 指定缓存 provider:spring.jpa....
这里,`hibernate.cache.use_second_level_cache`属性开启二级缓存,`hibernate.cache.region.factory_class`指定了Ehcache作为区域工厂,`net.sf.ehcache.configurationResourceName`则是Ehcache配置文件的位置。 ...
在Java企业级开发中,Spring和Hibernate是两个非常重要的框架,Spring作为一个全面的轻量级容器,负责管理和协调各种组件,而Hibernate则作为持久层框架,处理数据库操作。当涉及到高性能、大数据量的应用时,二级...
<property name="hibernate.cache.use_second_level_cache">true ``` 同时,还需要在`ehcache.xml`中配置与Hibernate相关的缓存区域。 **手动存储**: 在业务逻辑中,我们可以手动将数据存入和取出EHcache。例如,...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory <!-- entity class --> @Entity @...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 5. **实体类映射**:使用`...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider <!-- 其他缓存配置 --> </session-factory> </...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property> ``` 3. **配置Memcached客户端...
这个API文档是专门为那些使用中文作为主要语言的学习者设计的,使得理解Hibernate的核心概念和用法更加直观。以下是关于Hibernate 3 API的一些关键知识点: 1. **实体映射(Entity Mapping)**: Hibernate 3使用XML...
<prop key="hibernate.cache.use_second_level_cache">true <prop key="hibernate.cache.use_query_cache">true ``` 5. **事务管理**:确保你的事务配置支持读已提交(READ_COMMITTED)或可重复读...
Hibernate作为Java领域中广泛使用的对象关系映射(ORM)框架,其缓存机制是提高数据访问性能的关键组成部分。缓存能够减少对数据库的直接访问,从而提高应用性能,降低数据库负载。这里我们将深入探讨Hibernate的...
Hibernate二级缓存(Second Level Cache,简称SLC)是解决大量数据库操作性能问题的重要手段之一。此文档详细描述了如何在Spring与Hibernate整合的项目中启用Hibernate的二级缓存,并给出实例详解。 在介绍具体步骤...
**在Hibernate配置文件中启用二级缓存**:在hibernate.cfg.xml中,添加`<property name="hibernate.cache.use_second_level_cache">true</property>`来启用二级缓存,并通过`<property name="hibernate.cache.region...