JPA 二级缓存
JPA 2-level caching
抱歉提供了一个不太具体的标题。其实我很乱。
我的实际问题:提高应用程序的性能。
好事:数据是 inserted/updated 通过应用程序的 JPA。
目前使用的技术:Spring 3.2 框架与 JPA 2.0 和 hibernate 3.2。
到目前为止,我们的代码中没有任何地方直接依赖于 Hibernate。
回到问题:
我计划为某些始终获取相同数据(下拉值)的查询实施二级查询缓存。
我的第一个问题:JPA 是否自行提供二级缓存(不使用 EHcache 或任何此类依赖项)?
到目前为止我发现使用这个属性我们可以启用二级缓存
query.setHint("org.hibernate.cacheable", true);
我的第二个问题:我需要为 Ehcache 提供依赖还是 Hibernate-Ehcache 就足够了?
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.0.0</version>
</dependency>
或者我还需要提供
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcacheVersion}</version>
</dependency>
我的第三个问题:我需要在 persistence.xml 中添加哪些属性。
我确定这两个属性:
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
=====感谢 answer.This 更新可能会对开发人员有所帮助。====
我记下了启用 EHcache 需要定义的属性,因为很多人都面临这个异常
Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
因为 属性 不匹配和依赖项不匹配。
以下 属性 应与 Ehcache 2.4.3
一起用于休眠 4.x
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.cache.provider_configuration_file_resource_path" value="classpath:ehcache.xml" />
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
My 1st question :Does JPA provides 2nd level caching by itself (without
using EHcache or any such dependency)?
不,不是。 JPA 只是一个标准,不提供任何实现。 Hibernate 提供 JPA 的实现(EntityManager、EntityMangerFactory 等)及其自己的 ORM 实现(Session、Session Factory 等)。因此,需要 EHCache 支持二级缓存。但是,您可以使用 JPA annotations/config 进行缓存,但这需要在 persistence.xml.
中进行更改
以下两个链接解释了每个配置选项:
- 缓存使用 Hibernate specific classes/annotations.
- 缓存使用 JPA specific classes/annotations (with Hibernate as JPA provider)
My 2nd Question: Do I need to provide dependency for Ehcache or
Hibernate-Ehcache is enough?
您需要添加ehcache-core、hibernate-ehcache 和slf4j-simple(EHCache 使用slf4j 进行日志记录)。有关依赖项的详细信息,请查看此 link.
上的 Hibernate EHCache Maven 依赖项 部分
My third question: What are the properties I need to add in my
persistence.xml.
如果您采用 JPA 方式,那么 sample persistence.xml
将类似于:
<persistence-unit name="FooPu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
...
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
</properties>
</persistence-unit>
抱歉提供了一个不太具体的标题。其实我很乱。
我的实际问题:提高应用程序的性能。
好事:数据是 inserted/updated 通过应用程序的 JPA。
目前使用的技术:Spring 3.2 框架与 JPA 2.0 和 hibernate 3.2。
到目前为止,我们的代码中没有任何地方直接依赖于 Hibernate。
回到问题:
我计划为某些始终获取相同数据(下拉值)的查询实施二级查询缓存。
我的第一个问题:JPA 是否自行提供二级缓存(不使用 EHcache 或任何此类依赖项)?
到目前为止我发现使用这个属性我们可以启用二级缓存
query.setHint("org.hibernate.cacheable", true);
我的第二个问题:我需要为 Ehcache 提供依赖还是 Hibernate-Ehcache 就足够了?
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.0.0</version>
</dependency>
或者我还需要提供
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcacheVersion}</version>
</dependency>
我的第三个问题:我需要在 persistence.xml 中添加哪些属性。 我确定这两个属性:
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
=====感谢 answer.This 更新可能会对开发人员有所帮助。====
我记下了启用 EHcache 需要定义的属性,因为很多人都面临这个异常
Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
因为 属性 不匹配和依赖项不匹配。
以下 属性 应与 Ehcache 2.4.3
一起用于休眠 4.x<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.cache.provider_configuration_file_resource_path" value="classpath:ehcache.xml" />
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
My 1st question :Does JPA provides 2nd level caching by itself (without using EHcache or any such dependency)?
不,不是。 JPA 只是一个标准,不提供任何实现。 Hibernate 提供 JPA 的实现(EntityManager、EntityMangerFactory 等)及其自己的 ORM 实现(Session、Session Factory 等)。因此,需要 EHCache 支持二级缓存。但是,您可以使用 JPA annotations/config 进行缓存,但这需要在 persistence.xml.
中进行更改以下两个链接解释了每个配置选项:
- 缓存使用 Hibernate specific classes/annotations.
- 缓存使用 JPA specific classes/annotations (with Hibernate as JPA provider)
My 2nd Question: Do I need to provide dependency for Ehcache or Hibernate-Ehcache is enough?
您需要添加ehcache-core、hibernate-ehcache 和slf4j-simple(EHCache 使用slf4j 进行日志记录)。有关依赖项的详细信息,请查看此 link.
上的 Hibernate EHCache Maven 依赖项 部分My third question: What are the properties I need to add in my persistence.xml.
如果您采用 JPA 方式,那么 sample persistence.xml
将类似于:
<persistence-unit name="FooPu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
...
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
</properties>
</persistence-unit>