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

标签 hibernate caching jpa ehcache spring-data

我正在尝试但没有成功在 中缓存查询 Spring 数据 hibernate 环境如下 依赖项 :

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'

我的实体服务的 Spring Data Repository(ServiceRepository) 是
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);
}

正在调用存储库的 @Cacheable 方法
public List<Service> getAllServicesByStatusAndId(ServiceStatus status, List<Long> services, TransactorType transactor) {
    return serviceRepository.getAllMerchantServicesByStatusAndServiceId(status, services, transactor);
}

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

它的灵感来自 spring-data-jpa-examples/src/main/resources/caching-repository-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/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>

启用缓存的 hibernate 配置 (hibernate.cfg.xml) 是
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/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: 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

下次我请求时,我会看到以下查询;
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

引用

spring-projects/spring-data-jpa-examples

Spring 3.1 Caching and Config

Spring Data Rest - Caching

Spring Data Repository caching results

最佳答案

<cache:annotation-driven />jpa-context.xml丢失,添加解决问题的方法。最后jpa-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/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>

我还加了 resources/ehcache.xml .
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="http://ehcache.org/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>resources/hibernate.cfg.xml .

我的 CPU 缓存配置是
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

关于hibernate - Spring Data + Hibernate 查询缓存不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18872643/

相关文章:

java - JPA 查询或自定义查询根据两个表中的搜索词查找结果

mysql - 有没有办法在 HQL 中编写此 SQL?

java - 创建名称为 'entityManagerFactory' : [PersistenceUnit: default] Unable to build Hibernate SessionFactory 的 bean 时出错

.net - 根据数据库刷新刷新缓存

java - 立即重新填充 EhCache,而不是等待读取

c# - HttpClient 未在登台服务器上使用缓存

java - 来自 Controller 的 Spring Boot 查询

java - HQL递归,我该怎么做?

mysql - 如何创建 hibernate 不同的查询

java - 我捕获了异常,但仍然看到它在日志中抛出