hibernate sessionFactory配置 <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</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>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/onionportal/orm/SystemLog.hbm.xml</value>
</list>
</property>
</bean>
spring中是用AOP的方式加入EHCACHE的
<!-- 引用ehCache的配置 -->
<bean id="defaultCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>/WEB-INF/classes/ehcache.xml</value>
</property>
</bean>
<!-- 定义ehCache的工厂,并设置所使用的缓存名 -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager" />
</property>
<property name="cacheName">
<value>startCatche</value>
</property>
</bean>
<!-- query/find/create cache拦截器 -->
<bean id="methodCacheInterceptor" class="com.onionportal.until.ehcache.MethodCacheInterceptor">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>
<!-- flush cache拦截器 -->
<bean id="methodCacheAfterAdvice" class="com.onionportal.until.ehcache.MethodCacheAfterAdvice">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>
<!-- 定义切入点 -->
<bean id="methodCachePointCut"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheInterceptor" />
</property>
<property name="patterns">
<list>
<value>.*get.*</value>
<value>.*query.*</value>
<value>.*get.*</value>
<value>.*find.*</value>
</list>
</property>
</bean>
<bean id="methodCachePointCutAdvice"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheAfterAdvice" />
</property>
<property name="patterns">
<list>
<value>.*insert.*</value>
<value>.*update.*</value>
<value>.*delete.*</value>
</list>
</property>
</bean>
<!-- 事务管理 -->
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="query*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
<property name="preInterceptors"><!-- 这里加入ehCache缓存的切入点 -->
<list>
<ref bean="methodCachePointCut" />
<ref bean="methodCachePointCutAdvice" />
</list>
</property>
</bean>
</beans>
MethodCacheAfterAdvice 类public class MethodCacheAfterAdvice implements AfterReturningAdvice,
InitializingBean {
private static final Log logger = LogFactory.getLog(MethodCacheAfterAdvice.class);
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
public MethodCacheAfterAdvice() {
super();
}
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
String className = arg3.getClass().getName();
List list = cache.getKeys();
for(int i = 0;i<list.size();i++){
String cacheKey = String.valueOf(list.get(i));
if(cacheKey.startsWith(className)){
cache.remove(cacheKey);
logger.debug("remove cache " + cacheKey);
}
}
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");
}
} MethodCacheInterceptor 类public class MethodCacheInterceptor implements MethodInterceptor,
InitializingBean {
private static final Log logger = LogFactory.getLog(MethodCacheInterceptor.class);
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
public MethodCacheInterceptor() {
super();
}
/**
* 拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值,
* 否则,返回数据库查询结果,并将查询结果放入cache
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
Object result;
logger.debug("Find object from cache is " + cache.getName());
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null) {
logger.debug("Hold up method , Get method result and create cache........!");
result = invocation.proceed();
element = new Element(cacheKey, (Serializable) result);
cache.put(element);
}
return element.getValue();
} private String getCacheKey(String targetName, String methodName, Object[] arguments) {
StringBuffer sb = new StringBuffer();
sb.append(targetName).append(".").append(methodName);
if ((arguments != null) && (arguments.length != 0)) {
for (int i = 0; i < arguments.length; i++) {
sb.append(".").append(arguments[i]);
}
}
return sb.toString();
}
/**
* implement InitializingBean,检查cache是否为空
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");
}
} <class name="com.onionportal.orm.SystemLog" table="t_system_log" schema="onionportal">
<cache usage="read-write" region="startCatche"/>
在这里我也加上了这个usage的
Ehcache.xml的配置这个配置应该没什么问题吧<ehcache>
<diskStore path="java.io.tmpdir" />
<!--默认的缓存配置,在没有调用的情况下系统会自动调用此缓存-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
<!--
配置服务器启动时候需要配置好的缓存对象不需要经常该的!最大缓存数量为1000不写入磁盘, 永久有效,定时清理最少使用的内存对象
-->
<cache name="startCatche"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300000"
timeToLiveSeconds="600000"
overflowToDisk="true" />
</ehcache>查询时候的方法public void getOnionEhcache(){
try{
Session se=this.getSession();
se.beginTransaction();
Criteria query=se.createCriteria(SystemLog.class);
Criteria query1=se.createCriteria(Resources.class);
query1.list();
query.setCacheable(true);
List<SystemLog> li=new ArrayList<SystemLog>();
li=query.list();
for(int i=0;i<li.size();i++){
System.out.println(li.get(i).getMessageBody());
}
System.out.println(sessionFactory.getStatistics().toString());
System.out.println("输出的缓存"+sessionFactory.getStatistics().getSecondLevelCachePutCount());
System.out.println("找到的缓存"+sessionFactory.getStatistics().getSecondLevelCacheHitCount());
System.out.println("没找到的缓存"+sessionFactory.getStatistics().getSecondLevelCacheMissCount());
}catch (Exception e) {
System.out.println(e);
}
}输出的结果
Hibernate:
select
this_.ID as ID4_0_,
this_.r_name as r2_4_0_,
this_.r_url as r3_4_0_,
this_.r_describe as r4_4_0_,
this_.r_enabled as r5_4_0_,
this_.r_is_system as r6_4_0_
from
onionportal.t_resource this_
Hibernate:
select
this_.ID as ID0_0_,
this_.USER_ID as USER2_0_0_,
this_.MESSAGE_LEVEL as MESSAGE3_0_0_,
this_.STACK_TRACE as STACK4_0_0_,
this_.MESSAGE_BODY as MESSAGE5_0_0_,
this_.CREATE_TIME as CREATE6_0_0_
from
onionportal.t_system_log this_
Statistics[start time=1311523676343,sessions opened=0,sessions closed=0,transactions=0,successful transactions=0,optimistic lock failures=0,flushes=0,connections obtained=0,statements prepared=0,statements closed=0,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=0,entities updated=0,entities inserted=0,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
输出的缓存0
找到的缓存0
没找到的缓存0
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</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>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/onionportal/orm/SystemLog.hbm.xml</value>
</list>
</property>
</bean>
spring中是用AOP的方式加入EHCACHE的
<!-- 引用ehCache的配置 -->
<bean id="defaultCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>/WEB-INF/classes/ehcache.xml</value>
</property>
</bean>
<!-- 定义ehCache的工厂,并设置所使用的缓存名 -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager" />
</property>
<property name="cacheName">
<value>startCatche</value>
</property>
</bean>
<!-- query/find/create cache拦截器 -->
<bean id="methodCacheInterceptor" class="com.onionportal.until.ehcache.MethodCacheInterceptor">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>
<!-- flush cache拦截器 -->
<bean id="methodCacheAfterAdvice" class="com.onionportal.until.ehcache.MethodCacheAfterAdvice">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>
<!-- 定义切入点 -->
<bean id="methodCachePointCut"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheInterceptor" />
</property>
<property name="patterns">
<list>
<value>.*get.*</value>
<value>.*query.*</value>
<value>.*get.*</value>
<value>.*find.*</value>
</list>
</property>
</bean>
<bean id="methodCachePointCutAdvice"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheAfterAdvice" />
</property>
<property name="patterns">
<list>
<value>.*insert.*</value>
<value>.*update.*</value>
<value>.*delete.*</value>
</list>
</property>
</bean>
<!-- 事务管理 -->
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="query*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
<property name="preInterceptors"><!-- 这里加入ehCache缓存的切入点 -->
<list>
<ref bean="methodCachePointCut" />
<ref bean="methodCachePointCutAdvice" />
</list>
</property>
</bean>
</beans>
MethodCacheAfterAdvice 类public class MethodCacheAfterAdvice implements AfterReturningAdvice,
InitializingBean {
private static final Log logger = LogFactory.getLog(MethodCacheAfterAdvice.class);
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
public MethodCacheAfterAdvice() {
super();
}
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
String className = arg3.getClass().getName();
List list = cache.getKeys();
for(int i = 0;i<list.size();i++){
String cacheKey = String.valueOf(list.get(i));
if(cacheKey.startsWith(className)){
cache.remove(cacheKey);
logger.debug("remove cache " + cacheKey);
}
}
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");
}
} MethodCacheInterceptor 类public class MethodCacheInterceptor implements MethodInterceptor,
InitializingBean {
private static final Log logger = LogFactory.getLog(MethodCacheInterceptor.class);
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
public MethodCacheInterceptor() {
super();
}
/**
* 拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值,
* 否则,返回数据库查询结果,并将查询结果放入cache
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
Object result;
logger.debug("Find object from cache is " + cache.getName());
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null) {
logger.debug("Hold up method , Get method result and create cache........!");
result = invocation.proceed();
element = new Element(cacheKey, (Serializable) result);
cache.put(element);
}
return element.getValue();
} private String getCacheKey(String targetName, String methodName, Object[] arguments) {
StringBuffer sb = new StringBuffer();
sb.append(targetName).append(".").append(methodName);
if ((arguments != null) && (arguments.length != 0)) {
for (int i = 0; i < arguments.length; i++) {
sb.append(".").append(arguments[i]);
}
}
return sb.toString();
}
/**
* implement InitializingBean,检查cache是否为空
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");
}
} <class name="com.onionportal.orm.SystemLog" table="t_system_log" schema="onionportal">
<cache usage="read-write" region="startCatche"/>
在这里我也加上了这个usage的
Ehcache.xml的配置这个配置应该没什么问题吧<ehcache>
<diskStore path="java.io.tmpdir" />
<!--默认的缓存配置,在没有调用的情况下系统会自动调用此缓存-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
<!--
配置服务器启动时候需要配置好的缓存对象不需要经常该的!最大缓存数量为1000不写入磁盘, 永久有效,定时清理最少使用的内存对象
-->
<cache name="startCatche"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300000"
timeToLiveSeconds="600000"
overflowToDisk="true" />
</ehcache>查询时候的方法public void getOnionEhcache(){
try{
Session se=this.getSession();
se.beginTransaction();
Criteria query=se.createCriteria(SystemLog.class);
Criteria query1=se.createCriteria(Resources.class);
query1.list();
query.setCacheable(true);
List<SystemLog> li=new ArrayList<SystemLog>();
li=query.list();
for(int i=0;i<li.size();i++){
System.out.println(li.get(i).getMessageBody());
}
System.out.println(sessionFactory.getStatistics().toString());
System.out.println("输出的缓存"+sessionFactory.getStatistics().getSecondLevelCachePutCount());
System.out.println("找到的缓存"+sessionFactory.getStatistics().getSecondLevelCacheHitCount());
System.out.println("没找到的缓存"+sessionFactory.getStatistics().getSecondLevelCacheMissCount());
}catch (Exception e) {
System.out.println(e);
}
}输出的结果
Hibernate:
select
this_.ID as ID4_0_,
this_.r_name as r2_4_0_,
this_.r_url as r3_4_0_,
this_.r_describe as r4_4_0_,
this_.r_enabled as r5_4_0_,
this_.r_is_system as r6_4_0_
from
onionportal.t_resource this_
Hibernate:
select
this_.ID as ID0_0_,
this_.USER_ID as USER2_0_0_,
this_.MESSAGE_LEVEL as MESSAGE3_0_0_,
this_.STACK_TRACE as STACK4_0_0_,
this_.MESSAGE_BODY as MESSAGE5_0_0_,
this_.CREATE_TIME as CREATE6_0_0_
from
onionportal.t_system_log this_
Statistics[start time=1311523676343,sessions opened=0,sessions closed=0,transactions=0,successful transactions=0,optimistic lock failures=0,flushes=0,connections obtained=0,statements prepared=0,statements closed=0,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=0,entities updated=0,entities inserted=0,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
输出的缓存0
找到的缓存0
没找到的缓存0
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货