`
wang19841229
  • 浏览: 96954 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

spring+hibernate查询缓存的试验

    博客分类:
  • j2ee
阅读更多

今天打算把原来做的一个项目中的代码做一下优化和重构。

首先,从hibernate的查询做起。原来有一个查询在页面是多个页面都调用这个查询。而且查询条件是基本一样的。所以个人感觉应当应用hibernate的二级缓存和查询缓存。

步骤如下:

1:配置spring文件

在sessionfactiory的bean中加上如下配置

<prop key="hibernate.show_sql">true</prop>
打开二级缓存				
<prop key="hibernate.cache.use_second_level_cache">true</prop>
指定二级缓存的外部程序我用的是ECACHE
<propkey="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
打开hibernate的查询缓存
<prop key="hibernate.cache.use_query_cache">true</prop>

 2配置要使用缓存的实体

<hibernate-mapping>
    <class name="com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder" table="TjsEmailFolder" schema="tjsoadba" catalog="TJSOA">
关键是这里指定缓存的策略一般用读写就可以了,如果数据从不变化可以用只读
        <cache usage="read-write"></cache>
        <id name="folderId" type="java.lang.Integer">
            <column name="Folder_id" />
            <generator class="native" />
        </id>
        <many-to-one name="tjsUser" class="com.tjsinfo.tjsoa.system.TjsUser.TjsUser" fetch="select">
            <column name="Folder_userid" not-null="true" />
        </many-to-one>
        <property name="folderName" type="java.lang.String">
            <column name="Folder_name" length="100" not-null="true" />
        </property>
        <set name="tjsFoldMails" inverse="true">
            <key>
                <column name="tjsmail_flod_id" not-null="true" />
            </key>
            <one-to-many class="com.tjsinfo.tjsoa.mail.vo.TjsFoldMail" />
        </set>
    </class>
</hibernate-mapping>

 3定义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 for defaultCache:

        maxInMemory       - 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.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

   

Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The 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 memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        

   Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined 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"
        />
<cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> 
     Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    

    <!-- Place configuration for your caches following -->
    
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="false"
        />    
</ehcache>

 4执行相应的HQL语句,应当是第一次执行有SQL而第二次没有可是我试了一下还是有SQL说明查询缓存没用。我晕。

后来发现应当在调用查询的方法中的spring 的hibernate模板设置打开查询缓存。

 

public List selectHql(String hql) {
		// TODO Auto-generated method stub
		getHibernateTemplate().setCacheQueries(true);
		return getHibernateTemplate().find(hql);
	}

 这样就OK啦。

实践是检验一切真理的唯一标准。

分享到:
评论
1 楼 xuejianshan 2008-10-12  
可否把你的这点东西直接上传上去~~~

相关推荐

    网上书店JavaEE课程期末大型试验SSH架构

    SSH架构是Struts2、Spring和Hibernate三个开源框架的组合,它们共同构建了一个高效、灵活且可扩展的企业级应用框架。 1. **Struts2**:作为MVC(Model-View-Controller)框架,Struts2负责处理用户请求,管理业务...

    种鹅舍环境智能监控系统的研制和试验.pdf

    该系统的开发使用了EX、Hibernate和Spring为基础框架技术,构建了轻量级、强壮的多级缓存的J2EE企业级Web应用程序。该系统的试验结果表明,该智能监控系统长期运行稳定、可靠,能够满足鹅反季节繁殖对光照和温湿度的...

    试验上传试验上传试验上传试验上传

    7. **Ehcache-1.2.3.jar**: Ehcache 是一个广泛使用的Java缓存库,能够提高应用性能,通过存储常用数据到内存,避免频繁的数据库查询。在Hibernate中,Ehcache可以作为二级缓存提供者,进一步优化数据访问速度。 8....

    Activiti6.0.x-中文版用户指南

    - **部署缓存配置**:优化部署时的缓存行为。 4. **Activiti API**: - **Process Engine API和服务**:讲解如何使用API进行流程部署、启动、查询等操作。 - **异常策略**:处理流程执行中可能出现的异常情况。 ...

    activiti 5.16 流程管理 中文版

    - **试验性功能**:部分新特性可能处于试验阶段,不建议在生产环境中使用。 - **内部实现类**:文档中可能会提及一些关键的内部实现细节。 #### 2. 开始学习 - **一分钟入门**:快速了解如何安装和配置Activiti。 -...

    activiti5.16中文用户手册

    对于在使用过程中遇到的问题,手册会指导用户如何报告问题,并对试验性功能进行说明。 接下来是开始学习Activiti的步骤,包括了一分钟入门指南,安装Activiti及数据库,引入必要的jar包和依赖,并指导用户如何进行...

Global site tag (gtag.js) - Google Analytics