From 3883abf0879a34525138afa301529a9cc8dd8e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Wed, 4 Jul 2018 12:12:00 +0200 Subject: [PATCH] HHH-12649 Use the 'create-warn' missing cache strategy by default in hibernate-jcache In order to be consistent with hibernate-ehcache. --- .../userguide/chapters/caching/Caching.adoc | 8 +++---- .../cache/jcache/MissingCacheStrategy.java | 2 +- .../jcache/test/MissingCacheStrategyTest.java | 22 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc b/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc index 50a239862f..c12af7803f 100644 --- a/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc +++ b/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc @@ -563,9 +563,9 @@ Only by specifying the second property `hibernate.javax.cache.uri` will you be a ==== JCache missing cache strategy By default, the JCache region factory -will throw an exception when asked to retrieve a cache that is not pre-configured and pre-started in the underlying cache manager. +will log a warning when asked to create a cache that is not explicitly configured and pre-started in the underlying cache manager. Thus if you configure an entity type or a collection as cached, but do not configure the corresponding cache explicitly, -Hibernate ORM will fail to start. +one warning will be logged for each cache that was not configured explicitly. You may change this behavior by setting the `hibernate.javax.cache.missing_cache_strategy` property to one of the following values: @@ -574,8 +574,8 @@ to one of the following values: [cols=",",options="header",] |====================================== | Value | Description -|`fail` | **Default value**. Fail with an exception on missing caches. -|`create-warn` | Create a new cache when a cache is not found (see `create` below), +|`fail` | Fail with an exception on missing caches. +|`create-warn` | **Default value**. Create a new cache when a cache is not found (see `create` below), and also log a warning about the missing cache. |`create` | Create a new cache when a cache is not found, without logging any warning about the missing cache. |====================================== diff --git a/hibernate-jcache/src/main/java/org/hibernate/cache/jcache/MissingCacheStrategy.java b/hibernate-jcache/src/main/java/org/hibernate/cache/jcache/MissingCacheStrategy.java index b30f1bce86..b9ef31c95c 100644 --- a/hibernate-jcache/src/main/java/org/hibernate/cache/jcache/MissingCacheStrategy.java +++ b/hibernate-jcache/src/main/java/org/hibernate/cache/jcache/MissingCacheStrategy.java @@ -52,7 +52,7 @@ public enum MissingCacheStrategy { if ( StringHelper.isEmpty( externalRepresentation ) ) { // Use the default - return MissingCacheStrategy.FAIL; + return MissingCacheStrategy.CREATE_WARN; } for ( MissingCacheStrategy strategy : values() ) { diff --git a/hibernate-jcache/src/test/java/org/hibernate/jcache/test/MissingCacheStrategyTest.java b/hibernate-jcache/src/test/java/org/hibernate/jcache/test/MissingCacheStrategyTest.java index a5348be912..cb97b5153c 100644 --- a/hibernate-jcache/src/test/java/org/hibernate/jcache/test/MissingCacheStrategyTest.java +++ b/hibernate-jcache/src/test/java/org/hibernate/jcache/test/MissingCacheStrategyTest.java @@ -45,26 +45,22 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase { @Test public void testMissingCacheStrategyDefault() { - doTestMissingCacheStrategyFail( + doTestMissingCacheStrategyCreateWarn( ignored -> { } // default settings ); } @Test public void testMissingCacheStrategyFail() { - doTestMissingCacheStrategyFail( - builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "fail" ) - ); - } - - private void doTestMissingCacheStrategyFail(Consumer additionalSettings) { // first, lets make sure that the region names we think are non-existent really do not exist for ( String regionName : TestHelper.allDomainRegionNames ) { assertThat( TestHelper.getCache( regionName ), nullValue() ); } // and now let's try to build the standard testing SessionFactory, without pre-defining caches - try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory( additionalSettings ) ) { + try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory( + builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "fail" ) + ) ) { fail(); } catch (ServiceException expected) { @@ -98,6 +94,12 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase { @Test public void testMissingCacheStrategyCreateWarn() { + doTestMissingCacheStrategyCreateWarn( + builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "create-warn" ) + ); + } + + private void doTestMissingCacheStrategyCreateWarn(Consumer additionalSettings) { Map triggerables = new HashMap<>(); // first, lets make sure that the region names we think are non-existent really do not exist @@ -111,9 +113,7 @@ public class MissingCacheStrategyTest extends BaseUnitTestCase { ); } - try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory( - builder -> builder.applySetting( ConfigSettings.MISSING_CACHE_STRATEGY, "create-warn" ) - ) ) { + try ( SessionFactoryImplementor ignored = TestHelper.buildStandardSessionFactory( additionalSettings ) ) { for ( String regionName : TestHelper.allDomainRegionNames ) { // The caches should have been created automatically assertThat(