springではクエリーキャッシュを使用します。

20604 ワード

spring 3を使用していますので、スペックはspring 3に基づいています。
 
まず、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.myweb" />
	
	<!--jbpm4.4     -->
	<bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper"/>
	<bean id="processEngine" factory-bean="springHelper"  factory-method="createProcessEngine" />
	
	<!-- dataSourceproxy          -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"
		p:targetDataSource-ref="dynamicDataSource" />
	
	<!-- dataSource        -->
	<bean id="dynamicDataSource" class="com.myweb.support.DynamicDataSource">
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<entry key="dataSource" value-ref="dataSourceJDBC" />
			</map>
		</property>
	</bean>
	
	<!-- c3p0      -->
	<bean id="dataSourceJDBC" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close" p:driverClass="${jdbc.driverClass}" p: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"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</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/myweb/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" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<property name="mapperLocations" value="classpath*:com/myweb/ibatis/mapper/*.xml"/>
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.myweb.dao.ibatis" />
	</bean>

		<!-- spring transaction      -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" 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.myweb.*service..*(..))" order="0" />
	</aop:config>
	
	
	<!-- memcache-java -->
	<bean id="objectCache" class="com.myweb.service.ObjectCache">
		<property name="memCachedClient">
			<props>
				<prop key="memcached.servers">${memcached.object.servers}</prop>
				<prop key="memcached.weights">${memcached.object.weights}</prop>
				<prop key="memcached.failover">${memcached.failover}</prop>
				<prop key="memcached.initConn">${memcached.initConn}</prop>
				<prop key="memcached.minConn">${memcached.minConn}</prop>
				<prop key="memcached.maxConn">${memcached.maxConn}</prop>
				<prop key="memcached.maintSleep">${memcached.maintSleep}</prop>
				<prop key="memcached.nagle">${memcached.nagle}</prop>
				<prop key="memcached.socketTO">${memcached.socketTO}</prop>
				<prop key="memcached.aliveCheck">${memcached.aliveCheck}</prop>
			</props>
		</property>
	</bean>
</beans>
 
 主なBaseHDAの構成は以下の通りである。
 
package com.myweb.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
 *
 */
@Repository
public class BaseHDao extends HibernateDaoSupport {
	
	Log log = LogFactory.getLog(getClass());
	
	@Autowired
	private SessionFactory sessionFactory;

}
 
 
更にspringのデフォルトの設定を見にきてキャッシュを調べて、実は違いはhibernature Templateとhibernature Templateの中の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.myweb" />
	
	<!--jbpm4.4     -->
	<bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper"/>
	<bean id="processEngine" factory-bean="springHelper"  factory-method="createProcessEngine" />
	
	<!-- dataSourceproxy          -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"
		p:targetDataSource-ref="dynamicDataSource" />
	
	<!-- dataSource        -->
	<bean id="dynamicDataSource" class="com.myweb.support.DynamicDataSource">
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<entry key="dataSource" value-ref="dataSourceJDBC" />
			</map>
		</property>
	</bean>
	
	<!-- c3p0      -->
	<bean id="dataSourceJDBC" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close" p:driverClass="${jdbc.driverClass}" p: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"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</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/myweb/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" class="org.springframework.orm.hibernate3.HibernateTemplate">
       <property name="sessionFactory"><ref bean="sessionFactory"/></property>
       <property name="cacheQueries" value="true"></property>
	</bean>
	
	<!-- mybatis-spring    -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<property name="mapperLocations" value="classpath*:com/myweb/ibatis/mapper/*.xml"/>
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.myweb.dao.ibatis" />
	</bean>

		<!-- spring transaction      -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" 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.myweb.*service..*(..))" order="0" />
	</aop:config>
	
	
	<!-- memcache-java -->
	<bean id="objectCache" class="com.myweb.service.ObjectCache">
		<property name="memCachedClient">
			<props>
				<prop key="memcached.servers">${memcached.object.servers}</prop>
				<prop key="memcached.weights">${memcached.object.weights}</prop>
				<prop key="memcached.failover">${memcached.failover}</prop>
				<prop key="memcached.initConn">${memcached.initConn}</prop>
				<prop key="memcached.minConn">${memcached.minConn}</prop>
				<prop key="memcached.maxConn">${memcached.maxConn}</prop>
				<prop key="memcached.maintSleep">${memcached.maintSleep}</prop>
				<prop key="memcached.nagle">${memcached.nagle}</prop>
				<prop key="memcached.socketTO">${memcached.socketTO}</prop>
				<prop key="memcached.aliveCheck">${memcached.aliveCheck}</prop>
			</props>
		</property>
	</bean>
</beans>
 package comp.myweb.dao.hibernate;
 
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
 *
 */
@Repository
public class BaseHDao extends HibernateDaoSupport {
	
	Log log = LogFactory.getLog(getClass());
	
	@Autowired
	private HibernateTemplate hibernateTemplate;

}
 このように、springのHibernature Templateを使ってcacheQueriesをtrueに設定するということは、springデフォルトではクエリキャッシュを開くという設定であることが分かります。クエリーキャッシュは、クエリ文の結果をキャッシュし、そのライフサイクルは関連するマッピングエンティティオブジェクトが変動するまで、変動があれば再チェックしてキャッシュします。