Search code examples
javaspringhibernatecachingehcache

HHH90001006: Missing cache[default-update-timestamps-region] was created on-the-fly


A trivial Spring Boot 2.5 application with Spring Data JPA with EhCache 3 and Hibernate 5 with Query and Second Level Caches enabled:

spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true

generates a warning from Hibernate and an information message from EhCache for each cache on startup:

WARN org.hibernate.orm.cache          HHH90001006: Missing cache[default-update-timestamps-region] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.javax.cache.missing_cache_strategy' to 'create'.
INFO org.ehcache.core.EhcacheManager  Cache 'default-update-timestamps-region' created in EhcacheManager.

and

WARN org.hibernate.orm.cache          HHH90001006: Missing cache[default-query-results-region] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.javax.cache.missing_cache_strategy' to 'create'.
INFO org.ehcache.core.EhcacheManager  Cache 'default-query-results-region' created in EhcacheManager.

What's the proper EhCache configuration for these Hibernate caches?


Solution

  • Disable expiration on the default-update-timestamps-region cache as recommended by Hibernate:

    @Configuration @EnableCaching
    public class ApplicationConfiguration {
        @Bean
        public CacheManager ehCacheManager() {
            CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
    
            hibernateDefaultUpdateTimestampsRegionConfiguration(cacheManager);
            hibernateDefaultQueryResultsRegion(cacheManager);
    
            return cacheManager;
        }
    
        /**
         * Create Hibernate's default-update-timestamps-region cache. No expiration per Hibernate recommendation:
         * https://github.com/hibernate/hibernate-orm/blob/main/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc#query-cache-regions
         *
         * @param cacheManager
         */
        private void hibernateDefaultUpdateTimestampsRegionConfiguration(CacheManager cacheManager) {
            CacheConfigurationBuilder<Object, Object> builder = CacheConfigurationBuilder
                    .newCacheConfigurationBuilder(Object.class, Object.class,
                            ResourcePoolsBuilder.newResourcePoolsBuilder().offheap(1, MemoryUnit.MB));
    
            javax.cache.configuration.Configuration<Object, Object> cache = Eh107Configuration.fromEhcacheCacheConfiguration(builder);
    
            cacheManager.createCache("default-update-timestamps-region", cache);
    
            //cacheManager.createCache("default-update-timestamps-region", new MutableConfiguration<>());
        }
    
        /**
         * Create Hibernate's default-query-results-region cache.
         * https://github.com/hibernate/hibernate-orm/blob/main/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc#query-cache-regions
         *
         * @param cacheManager
         */
        private void hibernateDefaultQueryResultsRegion(CacheManager cacheManager) {
            CacheConfigurationBuilder<Object, Object> builder = CacheConfigurationBuilder
                    .newCacheConfigurationBuilder(Object.class, Object.class,
                            ResourcePoolsBuilder.newResourcePoolsBuilder().offheap(1, MemoryUnit.MB));
    
            javax.cache.configuration.Configuration<Object, Object> cache = Eh107Configuration.fromEhcacheCacheConfiguration(builder);
    
            cacheManager.createCache("default-query-results-region", cache);
    
            //cacheManager.createCache("default-query-results-region", new MutableConfiguration<>());
        }
    
    }