首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

spring中应用查询缓存

2012-09-27 
spring中使用查询缓存由于使用的是spring3所以一下配置都基于spring3.?先来看spring没有默认设置查询缓存

spring中使用查询缓存

由于使用的是spring3所以一下配置都基于spring3.

?

先来看spring没有默认设置查询缓存的设置,spring的xml如下:

?

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">  <context:property-placeholder location="classpath*:database.properties,classpath*:memcached.properties" />  <!-- spring注释自动注入 --> <context:annotation-config/> <context:component-scan base-package="com.masadora" /><!--jbpm4.4工作流  --><bean id="springHelper" factory-bean="springHelper"  factory-method="createProcessEngine" /><!-- dataSourceproxy 配置代理管理事务 --><bean id="dataSource"/><!-- dataSource 多数据源支持 --><bean id="dynamicDataSource" value-ref="dataSourceJDBC" /></map></property></bean><!-- c3p0数据源配置 --><bean id="dataSourceJDBC" p:driverp:jdbcUrl="${jdbc.jdbcUrl}"p:user="${jdbc.user}" p:password="${jdbc.password}" p:initialPoolSize="${c3p0.initialPoolSize}"p:minPoolSize="${c3p0.minPoolSize}" p:maxPoolSize="${c3p0.maxPoolSize}"p:acquireIncrement="${c3p0.acquireIncrement}" p:maxIdleTime="${c3p0.maxIdleTime}"p:maxStatements="${c3p0.maxStatements}" lazy-init="true" /><!-- hibernate-spring 基本配置 --><bean id="sessionFactory"/></property><property name="hibernateProperties"><props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop><prop key="hibernate.jdbc.fetch_size">50</prop><prop key="hibernate.jdbc.batch_size">25</prop><prop key="hibernate.cache.use_query_cache">true</prop><prop key="hibernate.cache.use_second_level_cache">true</prop><prop key="hibernate.max_fetch_depth">1</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop><prop key="hibernate.memcached.servers">${memcached.object.servers}</prop><prop key="hibernate.memcached.cacheTimeSeconds">86400</prop><prop key="hibernate.memcached.clearSupported">false</prop><prop key="hibernate.connection.release_mode">after_transaction</prop>            </props></property><property name="mappingLocations"><list><value>classpath*:com/masadora/modal/hibernate/*.hbm.xml</value><value>classpath*:jbpm.repository.hbm.xml</value><value>classpath*:jbpm.execution.hbm.xml</value><value>classpath*:jbpm.history.hbm.xml</value><value>classpath*:jbpm.task.hbm.xml</value><value>classpath*:jbpm.identity.hbm.xml</value></list></property><!-- 使用TransactionAwareDataSourceProxy管理事务与ibatis处于同一事务管理下 --><property name="useTransactionAwareDataSource" value="true"></property></bean><!-- mybatis-spring 配置 --><bean id="sqlSessionFactory" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml" /><property name="mapperLocations" value="classpath*:com/masadora/ibatis/mapper/*.xml"/></bean><bean value="com.masadora.dao.ibatis" /></bean><!-- spring transaction 事务管理 --><bean id="transactionManager"ref="dataSource" /></bean><tx:advice id="txManager" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/>            <tx:method name="*" propagation="REQUIRED"/></tx:attributes></tx:advice><aop:config><aop:advisor id="txAdvisor" advice-ref="txManager" pointcut="execution(* com.masadora.*service..*(..))" order="0" /></aop:config><!-- memcache-java --><bean id="objectCache" name="code">package com.masadora.dao.hibernate;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.annotation.Autowired;import org.hibernate.SessionFactory;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Repository;/** * @author jsczxy2 * */@Repositorypublic class BaseHDao extends HibernateDaoSupport {Log log = LogFactory.getLog(getClass());@Autowiredprivate SessionFactory sessionFactory;}

?

?

再来看spring默认设置查询缓存,其实差异就在hibernateTemplate和hibernateTemplate里的cacheQueries设置为true!

?

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">  <context:property-placeholder location="classpath*:database.properties,classpath*:memcached.properties" />  <!-- spring注释自动注入 --> <context:annotation-config/> <context:component-scan base-package="com.masadora" /><!--jbpm4.4工作流  --><bean id="springHelper" factory-bean="springHelper"  factory-method="createProcessEngine" /><!-- dataSourceproxy 配置代理管理事务 --><bean id="dataSource"/><!-- dataSource 多数据源支持 --><bean id="dynamicDataSource" value-ref="dataSourceJDBC" /></map></property></bean><!-- c3p0数据源配置 --><bean id="dataSourceJDBC" p:driverp:jdbcUrl="${jdbc.jdbcUrl}"p:user="${jdbc.user}" p:password="${jdbc.password}" p:initialPoolSize="${c3p0.initialPoolSize}"p:minPoolSize="${c3p0.minPoolSize}" p:maxPoolSize="${c3p0.maxPoolSize}"p:acquireIncrement="${c3p0.acquireIncrement}" p:maxIdleTime="${c3p0.maxIdleTime}"p:maxStatements="${c3p0.maxStatements}" lazy-init="true" /><!-- hibernate-spring 基本配置 --><bean id="sessionFactory"/></property><property name="hibernateProperties"><props>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop><prop key="hibernate.jdbc.fetch_size">50</prop><prop key="hibernate.jdbc.batch_size">25</prop><prop key="hibernate.cache.use_query_cache">true</prop><prop key="hibernate.cache.use_second_level_cache">true</prop><prop key="hibernate.max_fetch_depth">1</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop><prop key="hibernate.memcached.servers">${memcached.object.servers}</prop><prop key="hibernate.memcached.cacheTimeSeconds">86400</prop><prop key="hibernate.memcached.clearSupported">false</prop><prop key="hibernate.connection.release_mode">after_transaction</prop>            </props></property><property name="mappingLocations"><list><value>classpath*:com/masadora/modal/hibernate/*.hbm.xml</value><value>classpath*:jbpm.repository.hbm.xml</value><value>classpath*:jbpm.execution.hbm.xml</value><value>classpath*:jbpm.history.hbm.xml</value><value>classpath*:jbpm.task.hbm.xml</value><value>classpath*:jbpm.identity.hbm.xml</value></list></property><!-- 使用TransactionAwareDataSourceProxy管理事务与ibatis处于同一事务管理下 --><property name="useTransactionAwareDataSource" value="true"></property></bean><!-- hibernateTemplate 并使用查询缓存设置--><bean id="hibernateTemplate" value="true"></property></bean><!-- mybatis-spring 配置 --><bean id="sqlSessionFactory" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml" /><property name="mapperLocations" value="classpath*:com/masadora/ibatis/mapper/*.xml"/></bean><bean value="com.masadora.dao.ibatis" /></bean><!-- spring transaction 事务管理 --><bean id="transactionManager"ref="dataSource" /></bean><tx:advice id="txManager" transaction-manager="transactionManager"><tx:attributes><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/>            <tx:method name="*" propagation="REQUIRED"/></tx:attributes></tx:advice><aop:config><aop:advisor id="txAdvisor" advice-ref="txManager" pointcut="execution(* com.masadora.*service..*(..))" order="0" /></aop:config><!-- memcache-java --><bean id="objectCache" name="code">import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Repository;/** * @author jsczxy2 * */@Repositorypublic class BaseHDao extends HibernateDaoSupport {Log log = LogFactory.getLog(getClass());@Autowiredprivate HibernateTemplate hibernateTemplate;}

?这样可以看出使用spring的HibernateTemplate 来设置cacheQueries为true就是设置spring默认打开查询缓存。查询缓存会缓存查询语句的结果,其生命周期是到相关映射实体对象有变动为止,一旦有变动则会重新查询后缓存。

热点排行