Spring Data + Hibernate查询缓存不起作用

编程入门 行业动态 更新时间:2024-10-28 02:28:18
本文介绍了Spring Data + Hibernate查询缓存不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试但未成功缓存 Spring Data 和 Hibernate environmet中的查询,并使用以下依赖关系:

compile'org.hibernate:hibernate-validator:4.0.0.GA' compile'org.hibernate:hibernate-entitymanager:3.6。 6.Final' compile'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final' compile'org.hibernate:hibernate-ehcache:3.3.1。 GA' compile'org.springframework.data:spring-data-jpa:1.2.0.RELEASE'

实体服务的My Spring Data Repository(ServiceRepository)是

public interface ServiceRepository扩展CrudRepository< Service,Long> ,JpaSpecificationExecutor< Service> { @Cacheable(merchantServices) @Query(select s from Service s JOIN s.statusList sas where s.status =?1 and s.priviligedUser.priviligedUserType IN(2,4)and (s.id IN(从Service st inner join st.tags tag where select IN(?3))或s.serviceType IN(?2))中选择st.id,并且sas.active = true和sas.transactorType =?4 ORDER BY s.name) List< Service> getAllMerchantServicesByStatusTypeAndTags(ServiceStatus状态,List< ServiceType>类型,List< String>标签,TransactorType交易者); $ / code>

从中调用版本库的@Cacheable方法

public List< Service> getAllServicesByStatusAndId(ServiceStatus status,List< Long> services,TransactorType transactor){ return serviceRepository.getAllMerchantServicesByStatusAndServiceId(status,services,transactor); }

我的缓存confing(jpa-context.xml)是

它的灵感来源于 spring-data-jpa-examples / src / main / resources / caching-repository-context.xml

<?xml version =1.0encoding =UTF-8?> < beans xmlns:p =www.springframework/schema/p xmlns =www.springframework/schema/beans xmlns:xsi =www.w3/2001/XMLSchema-instance xmlns:cache =www.springframework/schema/cache xmlns:mvc =www.springframework/schema/mvc xmlns:context =www.springframework/schema/contextxmlns:tx = www.springframework/schema/tx xsi:schemaLocation =www.springframework/schema/beans www.springframework/schema/beans/ spring-beans-3.0.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.0.xsd http ://www.springframework/schema/context www.springframework/schema/context/spring-context-3.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx.xsd www.springframework/schema/ca che www.springframework/schema/cache/spring-cache.xsd default-autowire =byName> < tx:annotation-driven /> < bean id =cacheManagerclass =org.springframework.cache.support.SimpleCacheManager> <属性名称=缓存> < set> < bean class =org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBeanp:name =merchantServices/> < / set> < / property> < / bean> < / beans>

启用缓存Hibernate配置(hibernate.cfg.xml)为

<?xml version ='1.0'encoding ='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate配置DTD 3.0 // ENhibernate.sourceforge/hibernate-configuration-3.0 .dtd> < hibernate-configuration> < session-factory> < property name =hibernate.default_batch_fetch_size> 16< / property> < property name =hibernate.max_fetch_depth> 5< / property> < property name =hibernate.cache.use_query_cache> true< / property> < property name =hibernate.cache.use_second_level_cache> true< / property> < property name =hibernate.generate_statistics> true< / property> < property name =hibernate.cache.provider_class> org.hibernate.cache.EhCacheProvider< / property> < property name =hibernate.c3p0.timeout> 300< / property> < / session-factory> < / hibernate-configuration>

查看日志;每次我请求时,我都会看到执行的查询。

17:50:28.997 [http-bio-8080-exec-10] INFO org.hibernate.stat.Statistics - HQL:从Service s中选择s JOIN s.statusList sas其中s.status =?1和s.priviligedUser.priviligedUserType IN(2,4)和s.id IN(?2)和sas.active = true和sas.transactorType =?3 ORDER BY s.name,time:57ms,rows:1

下一次我请求时,我看到下面的查询;

18:03:40.374 [http-bio-8080- exec-8] INFO org.hibernate.stat.Statistics - HQL:从服务s中选择s JOIN s.statusList sas其中s.status =?1和s.priviligedUser.priviligedUserType IN(2,4)和s.id IN( ?2)和sas.active = true和sas.transactorType =?3 ORDER BY s.name,time:47ms,rows:1

参考文献

spring-projects / spring-data-jpa-examples

Spring Data Rest - 缓存

< Spring数据存储库缓存结果 解决方案

在 jpa-context.xml 中缺少

< cache:annotation-driven /> 解决了这个问题。最后的 jpa-context.xml 是

<?xml version =1.0encoding =UTF-8?> < beans xmlns:p =www.springframework/schema/p xmlns =www.springframework/schema/beans xmlns:xsi =www.w3/2001/XMLSchema-instance xmlns:cache =www.springframework/schema/cache xmlns:mvc =www.springframework/schema/mvc xmlns:context =www.springframework/schema/contextxmlns:tx = www.springframework/schema/tx xsi:schemaLocation =www.springframework/schema/beans www.springframework/schema/beans/ spring-beans-3.0.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.0.xsd http ://www.springframework/schema/context www.springframework/schema/context/spring-context-3.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx.xsd www.springframework/schema/ca che www.springframework/schema/cache/spring-cache.xsd default-autowire =byName> < tx:annotation-driven /> < cache:annotation-driven /> < bean id =cacheManagerclass =org.springframework.cache.support.SimpleCacheManager> <属性名称=缓存> < set> < bean class =org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBeanp:name =merchantServices/> < / set> < / property> < / bean> < / beans>

我还添加了 resources / ehcache.xml 。

<?xml version =1.0encoding =UTF-8?> < ehcache xmlns:xsi =www.w3/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation =ehcache/ehcache.xsd> ; < diskStore path =/ home / prayag / cache _/> < defaultCache eternal =false maxElementsInMemory =1000 overflowToDisk =true diskPersistent =true timeToLiveSeconds = 300 /> < / ehcache>

引用为< property name =hibernate.cache.provider_configuration_file_resource_path > ehcache.xml< / property> in resources / hibernate.cfg.xml 。

我的CPU高速缓冲存储器conf是

prayag @ prayag:〜/ hacker_ / draobkcalb $ sudo dmidecode -t cache [sudo]密码为prayag:#dmidecode 2.11 SMBIOS 2.5存在。 处理0x000A,DMI类型7,19字节高速缓存信息套接字标识:内部高速缓存配置:启用,未插入,1级操作模式:回写位置:内部安装大小:32 kB 最大大小:32 kB 支持的SRAM类型:同步安装的SRAM类型:同步速度:未知错误更正类型:未知系统类型:未知关联:未知 处理0x000B,DMI类型7,19字节高速缓存信息套接字名称:外部高速缓存配置:启用,未插入,级别2 操作模式:回写位置:外部安装大小: 2048 kB 最大容量:2048 kB 支持的SRAM类型:同步安装的SRAM类型:同步速度:未知错误修正类型:未知系统类型:未知相关性:未知

I am trying but not succeeding to cache a query in Spring Data and Hibernate environmet with following dependencies :

compile 'org.hibernate:hibernate-validator:4.0.0.GA' compile 'org.hibernate:hibernate-entitymanager:3.6.6.Final' compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final' compile 'org.hibernate:hibernate-ehcache:3.3.1.GA' compile 'org.springframework.data:spring-data-jpa:1.2.0.RELEASE'

My Spring Data Repository(ServiceRepository) for entity Service is

public interface ServiceRepository extends CrudRepository<Service, Long>, JpaSpecificationExecutor<Service> { @Cacheable("merchantServices") @Query("select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and (s.id IN (select st.id from Service st inner join st.tags tag where tag IN (?3)) or s.serviceType IN (?2)) and sas.active=true and sas.transactorType=?4 ORDER BY s.name") List<Service> getAllMerchantServicesByStatusTypeAndTags(ServiceStatus status, List<ServiceType> type, List<String> tags, TransactorType transactor); }

Repository's @Cacheable method being invoked from

public List<Service> getAllServicesByStatusAndId(ServiceStatus status, List<Long> services, TransactorType transactor) { return serviceRepository.getAllMerchantServicesByStatusAndServiceId(status, services, transactor); }

My Caching confing (jpa-context.xml) is

It is inspired from spring-data-jpa-examples/src/main/resources/caching-repository-context.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:p="www.springframework/schema/p" xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:cache="www.springframework/schema/cache" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xmlns:tx="www.springframework/schema/tx" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.0.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx.xsd www.springframework/schema/cache www.springframework/schema/cache/spring-cache.xsd" default-autowire="byName"> <tx:annotation-driven /> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="merchantServices"/> </set> </property> </bean> </beans>

cache enabled Hibernate config (hibernate.cfg.xml) is

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "hibernate.sourceforge/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.default_batch_fetch_size">16</property> <property name="hibernate.max_fetch_depth">5</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.generate_statistics">true</property> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="hibernate.c3p0.timeout">300</property> </session-factory> </hibernate-configuration>

Looking at log; every time I request, I see query executed.

17:50:28.997 [http-bio-8080-exec-10] INFO org.hibernate.stat.Statistics - HQL: select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and s.id IN (?2) and sas.active=true and sas.transactorType=?3 ORDER BY s.name, time: 57ms, rows: 1

Next time I request, I see following query;

18:03:40.374 [http-bio-8080-exec-8] INFO org.hibernate.stat.Statistics - HQL: select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and s.id IN (?2) and sas.active=true and sas.transactorType=?3 ORDER BY s.name, time: 47ms, rows: 1

References

spring-projects/spring-data-jpa-examples

Spring 3.1 Caching and Config

Spring Data Rest - Caching

Spring Data Repository caching results

解决方案

<cache:annotation-driven /> in jpa-context.xml was missing, adding which fixes the problem . The final jpa-context.xml is

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:p="www.springframework/schema/p" xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:cache="www.springframework/schema/cache" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xmlns:tx="www.springframework/schema/tx" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.0.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx.xsd www.springframework/schema/cache www.springframework/schema/cache/spring-cache.xsd" default-autowire="byName"> <tx:annotation-driven /> <cache:annotation-driven /> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="merchantServices"/> </set> </property> </bean> </beans>

I also added resources/ehcache.xml.

<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache/ehcache.xsd"> <diskStore path="/home/prayag/cache_"/> <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="true" diskPersistent="true" timeToLiveSeconds="300" /> </ehcache>

Which is referenced as <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property> in resources/hibernate.cfg.xml.

My CPU cache conf is

prayag@prayag:~/hacker_/draobkcalb$ sudo dmidecode -t cache [sudo] password for prayag: # dmidecode 2.11 SMBIOS 2.5 present. Handle 0x000A, DMI type 7, 19 bytes Cache Information Socket Designation: Internal Cache Configuration: Enabled, Not Socketed, Level 1 Operational Mode: Write Back Location: Internal Installed Size: 32 kB Maximum Size: 32 kB Supported SRAM Types: Synchronous Installed SRAM Type: Synchronous Speed: Unknown Error Correction Type: Unknown System Type: Unknown Associativity: Unknown Handle 0x000B, DMI type 7, 19 bytes Cache Information Socket Designation: External Cache Configuration: Enabled, Not Socketed, Level 2 Operational Mode: Write Back Location: External Installed Size: 2048 kB Maximum Size: 2048 kB Supported SRAM Types: Synchronous Installed SRAM Type: Synchronous Speed: Unknown Error Correction Type: Unknown System Type: Unknown Associativity: Unknown

更多推荐

Spring Data + Hibernate查询缓存不起作用

本文发布于:2023-11-22 17:25:52,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   不起作用   Spring   Data   Hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!