缓存关闭,在运行测试套件时导致异常

时间:2018-11-10 00:39:50

标签: ehcache spring-test jcache shared-cache

我遇到的问题类似于this question中所述。

我有一个测试套件,可以在开发环境中正常运行。在Bitbucket管道中执行时,其中一项测试失败,但以下情况除外:

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)
   ....

我想尝试accepted solution,但不知道如何将其应用于我的项目。 second solution取决于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”。如何针对我的配置执行此操作?

2 个答案:

答案 0 :(得分:0)

您正在讨论的建议解决方案基于Ehcache2。您正在使用Ehcache 3(对您有好处),因此无效。

Spring会负责关闭CachingProvider,因此通常情况下,您无需执行任何操作。另外,您不需要通过@Autowired访问它。您可以javax.cache.CacheManager CacheManager,这样就可以确保找到正确的对象。

但是,您正在使用Hibernate。您应该确保Spring和Hibernate使用相同的CacheManager。配置它的方式取决于Spring和Hibernate版本。

我们可以获取完整的堆栈跟踪信息吗?现在,感觉有些东西正在关闭CachingProvider,而没有从org.ehcache.CacheManager注销。除非关闭javax.cache.CacheManager而不关闭global包装它,否则这是不可能的。关闭后者将导致注销。

答案 1 :(得分:0)

一种选择是提供不共享CachingProvider -s的自定义CacheManager。可以找到here的示例解决方案。