`

hibernate memcached

 
阅读更多
配置memcached所需jar(这里采用hibernate-memcached.*.jar):
1.hibernate-memcached-1.2.2.jar
2.memcached-2.3.1.jar

在使用sping的情况下的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
	http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr/spring-dwr-2.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<!-- spring annotation声明 -->
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="com.sport"></context:component-scan>
	<!-- 声明AOP -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

	<!-- 声明可读取配置文件,在本XML中就可用${}取到xxx.properties文件的值 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:jdbc.properties"></property>
	</bean>

	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<value>${driver}</value>
		</property>
		<property name="url">
			<value>${url}</value>
		</property>
		<property name="username">
			<value>${username}</value>
		</property>
		<property name="password">
			<value>${password}</value>
		</property>
		<property name="maxActive">
			<value>${maxActive}</value>
		</property>
		<property name="minIdle">
			<value>${minIdle}</value>
		</property>
		<property name="maxIdle">
			<value>${maxIdle}</value>
		</property>
	</bean>
	<!-- 配置session工厂,这里用spring提供的AnnotationSessionFactoryBean -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 指定实体类扫描包 -->
		<property name="packagesToScan">
			<list>
				<value>com.sport.bean</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			
<!--hibernate memcached 配置-->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>
				<prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop>
				<prop key="hibernate.memcached.servers">localhost:11211</prop>
				<prop key="hibernate.memcached.cacheTimeSeconds">300</prop>
				
				
				<!--<prop key="hibernate.hbm2ddl.auto">create</prop> -->
				
		 </props>
		</property>
	</bean>
	<!-- 注入hibernateTemplate -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	

	<!-- 事务管理器 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory"></property>
	<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	
	<!--使用基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
	</beans>




接下来启动memcached client如图:




接下来启动tomcat:

2011-12-09 00:06:03.690 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2011-12-09 00:06:03.707 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@a7ae01

2011-12-9 0:06:09 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory WebDemo
2011-12-9 0:06:09 org.apache.coyote.http11.Http11AprProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2011-12-9 0:06:09 org.apache.coyote.ajp.AjpAprProtocol start
信息: Starting Coyote AJP/1.3 on ajp-8009
2011-12-9 0:06:09 org.apache.catalina.startup.Catalina start
信息: Server startup in 21838 ms

跟踪memcached输出信息如下:

附件中可下载相关jar和memcached client-windows。



  • 大小: 25.5 KB
  • 大小: 42.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics