From 26bf33abdc8587912e07eac26d82dfe33047a526 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 14 May 2018 13:19:27 -0500 Subject: [PATCH] HHH-12529 - Some StatisticsImpl methods throw an exception instead of returning null --- .../java/org/hibernate/stat/internal/StatisticsImpl.java | 9 +++++++-- .../java/org/hibernate/test/cache/RegionNameTest.java | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java index fccf57ecb2..04043a2d71 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java @@ -625,10 +625,12 @@ public class StatisticsImpl implements StatisticsImplementor, Service { return l2CacheStatsMap.computeIfAbsent( regionName, s -> { - final Region region = sessionFactory.getCache().getRegion( regionName ); + Region region = sessionFactory.getCache().getRegion( regionName ); if ( region == null ) { - throw new IllegalArgumentException( "Unknown cache region : " + regionName ); + // this is the pre-5.3 behavior. and since this is a pre-5.3 method it should behave consistently + // NOTE that this method is deprecated + region = sessionFactory.getCache().getQueryResultsCache( regionName ).getRegion(); } return new CacheRegionStatisticsImpl( region ); @@ -638,6 +640,9 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public CacheRegionStatisticsImpl getSecondLevelCacheStatistics(String regionName) { + if ( sessionFactory == null ) { + return null; + } return getCacheRegionStatistics( sessionFactory.getCache().unqualifyRegionName( regionName ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/RegionNameTest.java b/hibernate-core/src/test/java/org/hibernate/test/cache/RegionNameTest.java index c76b16e333..1e2fd00f94 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cache/RegionNameTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/RegionNameTest.java @@ -82,6 +82,9 @@ public class RegionNameTest extends BaseNonConfigCoreFunctionalTestCase { final NaturalIdCacheStatistics naturalIdCacheStatistics = stats.getNaturalIdCacheStatistics( regionName ); assert naturalIdCacheStatistics != null; + + final SecondLevelCacheStatistics dne = stats.getSecondLevelCacheStatistics( cachePrefix + ".does.not.exist" ); + assert dne != null; } // todo (5.3) : any other API I can think of that deals with region-name?