小编Knu*_*nu8的帖子

二级缓存在 Hibernate + Spring + JPA 和 EhCache 中不起作用

让我清楚地了解二级缓存。我的 Web 应用程序的基类中有一个查询。几乎每个操作都会调用此查询(我使用的是 Struts,这就是应用程序的设计方式,因此不能真正弄乱它),例如加载我的主页调用三个单独的 Struts 操作,并且为每个操作执行此查询。QueryDsl 形式的查询看起来像 Iterable<Event> eventsFromDb2 = eventRepository.findAll(EventExpressions.queryAllEvents()); ,在简化形式中看起来像Select e from Event e where e.deleted = false

这个查询占用了大约 10 秒的甜蜜时间,因此它使应用程序变得非常慢,因为它调用了 Web 应用程序的每个操作(CRUD)。根据我的理解,在启用二级缓存时,hibernate+ Spring orm 应该从缓存中获取结果并避免数据库请求。但是,它不起作用。persistence.xml 如下所示

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
                       http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
                       http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                       http://www.springframework.org/schema/tx
                       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">



<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="flyway">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="de.mm.moreevent.type" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
            <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="generateDdl" value="true" />
        </bean>
    </property>
<property name="jpaPropertyMap">
    <map>
        <entry key="hibernate.cache.use_query_cache" value="true" />
        <entry key="hibernate.cache.use_second_level_cache" …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa ehcache second-level-cache spring-orm

6
推荐指数
1
解决办法
5373
查看次数

标签 统计

ehcache ×1

hibernate ×1

jpa ×1

second-level-cache ×1

spring-orm ×1