ehcache - 运行测试套件时缓存关闭导致异常
问题描述
我遇到了类似于此问题中描述的问题。
我有一个在开发环境中运行良好的测试套件。在 Bitbucket Pipelines 中执行时,其中一项测试失败,但有以下异常:
org.springframework.dao.InvalidDataAccessApiUsageException: Cache[model.Role] is closed; nested exception is java.lang.IllegalStateException: Cache[model.Role] is closed
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:364)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
....
我想尝试接受的解决方案,但我不知道如何将它应用到我的项目中。第二种解决方案取决于 ehcache.xml 文件。我没有这个文件,一切都在 JavaConfig 中配置。如何在 JavaConfig 中采用EhCache + JCache (JSR-107) 的建议解决方案?
我的缓存配置:
@Configuration
@EnableCaching
public class CacheConfig {
private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration =
Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(100, EntryUnit.ENTRIES))
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60)))
.build());
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer() {
return cm -> {
createIfNotExists(cm, "model.Role");
createIfNotExists(cm, "model.User.roles");
// ...
};
}
private void createIfNotExists(CacheManager cacheManager, String cacheName) {
if (cacheManager.getCache(cacheName) == null) {
cacheManager.createCache(cacheName, jcacheConfiguration);
}
}
}
摇篮依赖:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache'
implementation group: 'javax.cache', name: 'cache-api'
implementation group: 'org.ehcache', name: 'ehcache'
implementation group: 'org.hibernate', name: 'hibernate-jcache'
失败的测试:
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SecondLevelCacheTest {
@Autowired
private RoleRepository roleRepository;
private CacheManager manager;
@Before
public void initCacheManager() {
CachingProvider provider = Caching.getCachingProvider();
manager = provider.getCacheManager();
final String cacheRegion = "model.Role";
manager.getCache(cacheRegion).clear();
}
@Test
public final void givenEntityIsLoaded_thenItIsCached() {
final String cacheRegion = "model.Role";
boolean hasNext = manager.getCache(cacheRegion).iterator().hasNext();
final Role role = roleRepository.findByName("USER");
boolean hasNext2 = manager.getCache(cacheRegion).iterator().hasNext();
final Role role2 = roleRepository.findByName("USER");
Assert.assertFalse(hasNext);
Assert.assertTrue(hasNext2);
}
}
最受欢迎的解决方案是“在测试上下文中将共享属性设置为 false”。关于我的配置,我该如何做到这一点?
解决方案
一种选择是提供CachingProvider
不共享CacheManager
-s 的自定义。可以在此处找到示例解决方案。
推荐阅读
- mysql - 执行时间长。我要索引什么?
- python-3.x - smtplib.SMTP('smtp.office365.com', 587) 挂在 Windows Server 2012 上
- java - Java-Classnames 中前缀 I 的含义
- c# - API POST 请求“变成”GET 请求(Azure AD)
- delphi-7 - Delphi 7:如何在 TMemo 框中插入一行到 TEdit
- c# - 在 C# 中传递 List 元素
- javascript - 长文本溢出到 HTML/CSS 中的下一个组件
- angular - 有没有办法在 Kendo Grid for Angular 中有 2 个选定行的集合
- google-bigquery - 我正在寻找访问过这两个地点(圣克拉拉,米尔皮塔斯)的客户
- r - R-studio 中的循环