Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JPA&x2B;弹簧&x2B;冬眠+;DBUnit:可缓存查询:如何强制重新加载?_Spring_Hibernate_Jpa_Ehcache_Dbunit - Fatal编程技术网

Spring JPA&x2B;弹簧&x2B;冬眠+;DBUnit:可缓存查询:如何强制重新加载?

Spring JPA&x2B;弹簧&x2B;冬眠+;DBUnit:可缓存查询:如何强制重新加载?,spring,hibernate,jpa,ehcache,dbunit,Spring,Hibernate,Jpa,Ehcache,Dbunit,我正在尝试将我的项目设置为在junit模式下使用Spring+hibernate和Spring+hibernate+JTA(Glassfish)时使用缓存(ehcache) 这里是我的junit的spring hiberrnate配置 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheMa

我正在尝试将我的项目设置为在junit模式下使用Spring+hibernate和Spring+hibernate+JTA(Glassfish)时使用缓存(ehcache)

这里是我的junit的spring hiberrnate配置

<bean id="cacheManager" 
    class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="springEhcache"/>
</bean>
<bean id="springEhcache" 
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="cacheManagerName" value="method-cache" />
</bean>

<!-- Config Database: ImageManager -->
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/jdbc.properties" />

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
    p:password="${jdbc.password}" />


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean> 

我设置了一些查询:setCacheable(true)

编辑2。我删除了建议的行,并将其替换为

sessionFactory.getCache().evictQueryRegions();
sessionFactory.getCache().evictDefaultQueryRegion();
从我的配置中删除Spring缓存,现在它可以用于查询,但缓存级别2中没有实体。我将继续测试并发布完整的解决方案

统计数字如下:

无缓存 统计=统计[开始时间=1388275898144,打开的会话=16,关闭的会话=16,事务=32,成功的事务=16,乐观锁失败=0,刷新=87,获得的连接=16,准备的语句=1518,关闭的语句=1518,二级缓存放置=0,二级缓存命中=0,加载的实体=943,更新的实体=3,插入的实体]rted=334,删除的实体=0,取回的实体=64,加载的集合=459,更新的集合=149,删除的集合=0,重新创建的集合=200,取回的集合=459,对数据库执行的查询=422,查询缓存放置=0,查询缓存命中=0,查询缓存未命中=0,最大查询时间=321]

带缓存 统计=统计[开始时间=1388275940458,打开的会话=16,关闭的会话=16,事务=32,成功的事务=16,乐观锁失败=0,刷新=84,获得的连接=16,准备的语句=1341,关闭的语句=1341,二级缓存放置=0,二级缓存命中=0,加载的实体=841,更新的实体=3,插入的实体]rted=334,删除的实体=0,取回的实体=93,加载的集合=403,更新的集合=149,删除的集合=0,重新创建的集合=200,取回的集合=293,对数据库执行的查询=382,查询缓存放置=231,查询缓存命中=40,查询缓存未命中=231,最大查询时间=137]

编辑3:

我找到了如何将实体缓存到二级缓存中。缓存实体时速度似乎较慢

高速缓存:读写 统计=统计[开始时间=13882785470,打开的会话=16,关闭的会话=16,事务=32,成功的事务=16,乐观锁失败=0,刷新=84,获得的连接=16,准备的语句=1377,关闭的语句=1377,二级缓存放置=37,二级缓存命中=77,二级缓存未命中=2,加载的实体=764,更新的实体=3,实体在serted=334,entities deleted=0,entities fetched=38,collections loaded=403,collections updated=149,collections removed=0,collections recreated=200,collections fetched=403,query cache put=233,query cache hits=59,query cache misses=233,max query time=161]

无实体缓存 统计=统计[开始时间=1388278655759,打开的会话=16,关闭的会话=16,事务=32,成功的事务=16,乐观锁失败=0,刷新=84,获得的连接=16,准备的语句=1333,关闭的语句=1333,二级缓存放置=0,二级缓存命中=0,加载的实体=841,更新的实体=3,插入的实体]rted=334,删除的实体=0,获取的实体=104,加载的集合=403,更新的集合=149,删除的集合=0,重新创建的集合=200,获取的集合=293,对数据库执行的查询=363,查询缓存放置=233,查询缓存命中=59,查询缓存未命中=233,最大查询时间=147]

这里是我的ehcache.xml

<ehcache>
    <diskStore path="./resource"/>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
    />

    <cache name="org.hibernate.cache.StandardQueryCache"
        maxElementsInMemory="20"
        eternal="false"
        timeToLiveSeconds="120"
        overflowToDisk="false"/>

    <cache name="org.hibernate.cache.UpdateTimestampsCache"
        maxElementsInMemory="1000"
        eternal="true"
        overflowToDisk="false"/>
</ehcache>

这里是我的pom(它的一部分)


3.2.5.1发布
3.6.9.最终版本
org.springframework
弹簧芯
${spring.version}
org.springframework
春豆
${spring.version}
org.springframework
弹簧网
${spring.version}
org.hibernate
冬眠核心
${hibernate.version}
org.hibernate
休眠实体管理器
${hibernate.version}
org.hibernate
休眠ehcache
${hibernate.version}
net.sf.ehcache
ehcache内核
2.6.6
org.hibernate
休眠注释
3.5.6-最终版本
测验
org.dbunit
单元测试
2.4.9
测验
org.springframework
SpringWebMVC
${spring.version}
org.springframework
spring上下文
${spring.version}
org.springframework
spring上下文支持
${spring.version}
org.springframework
春季甲虫
${spring.version}
org.springframework
德克萨斯州春季
${spring.version}
org.apache.xbean
xbean spring
3.7

您需要获取cacheManager,然后获取所有缓存并将其删除

下面的代码示例将告诉您如何实现这一点:

EhCacheCacheManager cacheManager;   // injected cacheManger

String[] names = cacheManager.getCacheNames();

for (String name : names)
{
    Cache cache = cacheManager.getCache(name);

    cache.removeAll();
}
我必须这么做

currentSessionFactory = sessionFactory;
sessionFactory.getStatistics().isStatisticsEnabled();

try {
    sessionFactory.getCache().evictQueryRegions();
    sessionFactory.getCache().evictDefaultQueryRegion();
} catch (Exception e) {
}
并重新加载我的实体。效果很好

我的实体也在hibernate.xml中

<class-cache usage="read-write" class="entity.Platform" />

谢谢,但它不起作用。如果我重新运行查询,它仍然不会返回任何结果
<properties>
    <spring.version>3.2.5.RELEASE</spring.version>
    <hibernate.version>3.6.9.Final</hibernate.version>
</properties>

<!-- Spring 3 dependencies -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate.version}</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate.version}</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>${hibernate.version}</version>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.6.6</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.6-Final</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.dbunit</groupId>
    <artifactId>dbunit</artifactId>
    <version>2.4.9</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.xbean</groupId>
    <artifactId>xbean-spring</artifactId>
    <version>3.7</version>
</dependency>
EhCacheCacheManager cacheManager;   // injected cacheManger

String[] names = cacheManager.getCacheNames();

for (String name : names)
{
    Cache cache = cacheManager.getCache(name);

    cache.removeAll();
}
currentSessionFactory = sessionFactory;
sessionFactory.getStatistics().isStatisticsEnabled();

try {
    sessionFactory.getCache().evictQueryRegions();
    sessionFactory.getCache().evictDefaultQueryRegion();
} catch (Exception e) {
}
<class-cache usage="read-write" class="entity.Platform" />