java Spring Hibernate ehcache 设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5364752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring hibernate ehcache setup
提问by Johan Sj?berg
I have some problems getting the hibernate second level cache to work for caching domain objects. According to the ehcache documentation
it shouldn't be too complicated to add caching to my existing working application.
我在使休眠二级缓存用于缓存域对象时遇到了一些问题。根据ehcache documentation
将缓存添加到我现有的工作应用程序应该不会太复杂。
I have the following setup (only relevant snippets are outlined):
我有以下设置(仅概述了相关的片段):
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE
public void Entity {
// ...
}
ehcache-entity.xml
ehcache-entity.xml
<cache name="com.company.Entity" eternal="false"
maxElementsInMemory="10000" overflowToDisk="true" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
ApplicationContext.xml
应用上下文.xml
<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="ds" />
<property name="annotatedClasses">
<list>
<value>com.company.Entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache-entity.xml</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
....
</property>
</bean>
Maven dependencies
Maven 依赖项
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
<exclusions>
<exclusion>
<artifactId>hibernate</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.3.2</version>
</dependency>
A test class is used which enables cache statistics:
使用启用缓存统计的测试类:
Cache cache = cacheManager.getCache("com.company.Entity");
cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
cache.setStatisticsEnabled(true);
// store, read etc ...
cache.getStatistics().getMemoryStoreObjectCount(); // returns 0
No operation seems to trigger any cache changes. What am I missing? Currently I'm using HibernateTemplate
in the DAO, perhaps that has some impact.
似乎没有操作会触发任何缓存更改。我错过了什么?目前我HibernateTemplate
在 DAO 中使用,也许这有一些影响。
[EDIT]
[编辑]
The only ehcache log output when set to DEBUG is:
设置为 DEBUG 时唯一的 ehcache 日志输出是:
SettingsFactory: Cache region factory : net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
采纳答案by Johan Sj?berg
There were several causes that I've identified:
我已经确定了几个原因:
Correct maven dependencies:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.3.Final</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.4.1</version> </dependency>
Added the
@Cacheable
annotation fromjavax.persistence
to my entities.Read logging from hibernate instead of ehcache.
getSessionFactory().getStatistics().logSummary();
Not all hibernate operations seems to affect the cache. This I need to read up on further.
更正 Maven 依赖项:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.3.Final</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.4.1</version> </dependency>
将
@Cacheable
注释添加javax.persistence
到我的实体。从休眠而不是 ehcache 读取日志记录。
getSessionFactory().getStatistics().logSummary();
并非所有休眠操作似乎都会影响缓存。这我需要进一步阅读。
回答by gutch
Do you need to manually tell Hibernate to use the EHCache provider? I've never really been sure if this is required, but Hibernate does support a number of cache providers so I suspect that it might be necessary to explicitly tell Hibernate which one you want. Try adding this property to ApplicationContext.xml:
您是否需要手动告诉 Hibernate 使用 EHCache 提供程序?我从来没有真正确定是否需要这样做,但是 Hibernate 确实支持许多缓存提供程序,因此我怀疑可能有必要明确告诉 Hibernate 您想要哪个缓存提供程序。尝试将此属性添加到ApplicationContext.xml:
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
回答by Hailei Zhang
you can reference following configure
您可以参考以下配置
<prop key="hibernate.cache.use_query_cache">true</prop>
回答by Taras Melnyk
In hibernate.cfg.xml
add:
另外hibernate.cfg.xml
:
<hibernate-configuration>
<session-factory>
...
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
回答by Alex Snaps
By looking at your config, it all seems fine afaict. Only thing worth noticing, is that when using the HibernateTemplate, you have to explicitly setCacheQueries(true) if you plan on using the query cache... Which I wouldn't recommend, except if you reallyneed it: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html#setCacheQueries(boolean)
通过查看您的配置,一切似乎都很好。唯一值得注意的是,在使用 HibernateTemplate 时,如果您计划使用查询缓存,则必须显式 setCacheQueries(true)...我不建议这样做,除非您确实需要它: http://static .springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html#setCacheQueries(boolean)
Did you try the Hibernate statistics instead of the Ehcache ones ? Do you get cache misses there ? (reason I ask is to be sure you do use the same CacheManager as Hibernate does)...
您是否尝试过使用 Hibernate 统计信息而不是 Ehcache 统计信息?你那里有缓存未命中吗?(我问的原因是确保您确实使用与 Hibernate 相同的 CacheManager)...