运行几次弹簧测试时,Hibernate二级缓存已关闭 [英] Hibernate second level cache is closed while running a couple of spring tests

查看:159
本文介绍了运行几次弹簧测试时,Hibernate二级缓存已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为基于Hibernate 5.3和Spring Boot 2.1.3并使用Hibernate二级缓存的应用程序编写测试。

I'm trying to write tests for application which is working based on Hibernate 5.3 and Spring Boot 2.1.3 and using Hibernate second level cache.

m执行一批测试,这些测试正在设置spring上下文并尝试更新某些JPA实体,有时会出现如下异常:

When I'm executing batch of test which are setting up spring context and trying to update some JPA entity, at some point getting the exception such this:

org.springframework.dao.InvalidDataAccessApiUsageException: Cache[default-update-timestamps-region] is closed; nested exception is java.lang.IllegalStateException: Cache[default-update-timestamps-region] is closed

at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:370)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:536)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:533)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy244.save(Unknown Source)

我对Hibernate二级缓存具有以下配置:

I've the following configuration for Hibernate second level cache:

spring.jpa.properties.hibernate.cache.use_second_level_cache = true
spring.jpa.properties.hibernate.cache.use_query_cache = true spring.jpa.properties.hibernate.cache。 region.factory_class = org.hibernate.cache.jcache.JCacheRegionFactory
spring.jpa.properties.javax.persistence.sharedCache.mode = ENABLE_SELECTIVE

并使用Hibernate JCache作为依赖项。

and using Hibernate JCache as dependency.

从我了解到, org.hibernate.cache.jcache.JCacheRegionFactory 将EhCache CacheManager的相同实例重新用于Spring Test创建的所有上下文,但是一段时间后,Spring关闭缓存了上下文,这导致了

From what I understand, org.hibernate.cache.jcache.JCacheRegionFactory reuse same instance of EhCache CacheManager for all contexts created by Spring Test but after some of time Spring shutdown cached context which is causing closing CacheManager and caches.

以前,Hibernate(Hibernate EhCache模块)提供了org.hibernate.cache.ehcache.EhCacheRegionFactory工厂,该工厂每次都在创建新的CacheManager。有上述问题。

Previously, Hibernate (Hibernate EhCache module) provided org.hibernate.cache.ehcache.EhCacheRegionFactory factory which is creating new CacheManager every time and don't have problem described above.

是否有人知道如何为每个Spring测试上下文创建新的CacheManager并避免使用共享的上下文?

Does anyone know how to create new CacheManager for each Spring test context and avoid using shared one ?

推荐答案

此问题的可能解决方法是添加 @DirtiesContext 到您的班级:

A possible workaround for this problem is adding @DirtiesContext like this to your class:

@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
public class SomeTestClass {
...
}

这将迫使Spring为所有对象创建新的应用程序上下文此类的方法。就我而言,这解决了问题。

This will force Spring to create a new application context for all methods of this class. In my case, this resolved the issue.

另一种方法是确保Spring知道Hibernate缓存管理器。可以像本博客文章中所述>中所述来实现。但是,在某些情况下这可能是不可能的。

Another approach is to ensure that Spring is aware of the Hibernate cache manager. This can be achieved like described in this blog post. However, this might not be possible in some cases.

这篇关于运行几次弹簧测试时,Hibernate二级缓存已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆