naX*_*aXa 6 ehcache spring-test shared-cache jcache
我遇到了与此问题中描述的类似的问题。
我有一个在开发环境中运行良好的测试套件。在 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)
....
Run Code Online (Sandbox Code Playgroud)
我想尝试接受的解决方案,但我不知道如何将其应用到我的项目中。第二种解决方案依赖于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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
梯度依赖:
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'
Run Code Online (Sandbox Code Playgroud)
失败的测试:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
获得最多支持的解决方案是“在测试上下文中将共享属性设置为 false”。对于我的配置,我该如何做到这一点?
您所讨论的建议解决方案基于 Ehcache 2。您正在使用 Ehcache 3(对您有好处),因此它无效。
Spring 会负责关闭CacheManager,所以通常情况下,您不需要处理任何事情。此外,您不需要通过CachingProvider. 您可以@Autowired这样javax.cache.CacheManager,这样您就一定能找到合适的。
但是,您正在使用 Hibernate。您应该确保 Spring 和 Hibernate 使用相同的CacheManager. 配置方式取决于Spring和Hibernate版本。
我们可以获得完整的堆栈跟踪吗?现在感觉好像有什么东西正在关闭 ,但CacheManager没有从 中取消注册CachingProvider。这是不可能的,除非您在org.ehcache.CacheManager不关闭javax.cache.CacheManager包装的情况下关闭它。关闭后者将导致注销。
| 归档时间: |
|
| 查看次数: |
5136 次 |
| 最近记录: |