有了Ehcache Annotations for Spring还需要配置org.hibernate.cache.EhCacheProvider吗?

问一个白痴问题。

有了Ehcache Annotations for Spring还需要配置org.hibernate.cache.EhCacheProvider吗?

两者有区别和联系不?

应该需要,这两个属于不同层,多层架构基本三层,表现界面层 业务层和持久层。

首先感谢banq的快速回复。

Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法),则刷新cache中相应的内容,从使用的角度,感觉两者是可以相互替换的,也即使用哪一个都无妨,只是配置方法和由谁接管而已,也即是spring来接管还是hibernate来接管。个人观点,不知对不对,所以那到这里来讨论一下,听听大家的意见。

2011年03月08日 18:45 "zoff2002"的内容
感觉两者是可以相互替换的,也即使用哪一个都无妨,只是配置方法和由谁接管而已,也即是spring来接管还是hibernate来接管 ...

其实是一样的,关键问题是:ehcache被hibernate的封装其内部,在Spring中配置的是Hibernate的属性,而不是将其二级缓存直接作为Bean来管理,如:


<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>

如果Hibernate是基于Spring的IOC容器,正如Struts2.x可以使用Spring 的IOC容器一样,那么两者可以直接使用一个ehcache。

从这个方面看出,Hibernate不是非常透明,所谓设计的透明性就是其内部组件可以全部拆开打散。

2011年03月08日 19:47 "banq"的内容
如果Hibernate是基于Spring的IOC容器,正如Struts2.x可以使用Spring 的IOC容器一样,那么两者可以直接使用一个ehcache。 ...

嗯,谢谢banq,目前使用组合spring mvc +spring3.0.5 + hibernate3.6 + Ehcache annotation for spring,就没有配置org.hibernate.cache.EhCacheProvider,还在开发,暂时没有发现什么问题。只是有点担心而已,所有这里问问哈。

我把配置信息也贴出来吧


<?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:context="http://www.springframework.org/schema/context"
xmlns:p=
"http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:ehcache=
"http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-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:
//ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http:
//ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<import resource=
"security_config/spring-security.xml" />

<ehcache:annotation-driven cache-manager=
"ehCacheManager" />

<bean id=
"ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name=
"configLocation" value="classpath:ehcache.xml" />
</bean>
<!--
1、对这个PO做find ,list等方法前添加注释@Cacheable(cacheName=
"testCache") 返回值就可以被缓存起来;
2、对这个PO做update ,save,delete等方法前添加注释@TriggersRemove(cacheName=
"testCache",removeAll=true)
把缓存全部清除掉,以达到缓存数据和数据库中的数据实现同步;
-->
<!-- Spring Configuration -->


....

<bean id=
"sessionFactory"
class=
"org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name=
"dataSource" ref="dataSource"></property>
<property name=
"hibernateProperties">
<props>
<prop key=
"hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key=
"hibernate.show_sql">true</prop>
<prop key=
"hibernate.format_sql">true</prop>
<prop key=
"hibernate.jdbc.fetch_size">50</prop>
<prop key=
"hibernate.jdbc.batch_size">30</prop>
<prop key=
"hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key=
"hibernate.cache.use_second_level_cache">true</prop>
<prop key=
"hibernate.cache.use_query_cache">true</prop>
<prop key=
"hibernate.connection.release_mode">after_transaction</prop>
</props>
</property>

...

[该贴被zoff2002于2011-03-17 16:31修改过]