HHH-15105 Test and fix for NPE when access default query cache region statistics

This commit is contained in:
Christian Beikov 2022-03-08 22:28:08 +01:00
parent 37ef0627c9
commit 83b6e6e3d5
2 changed files with 118 additions and 0 deletions

View File

@ -491,6 +491,10 @@ public class EnabledCaching implements CacheImplementor, DomainDataRegionBuildin
return null;
}
if ( regionName == null || regionName.equals( getDefaultQueryResultsCache().getRegion().getName() ) ) {
return getDefaultQueryResultsCache();
}
return namedQueryResultsCacheMap.get( regionName );
}

View File

@ -0,0 +1,114 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.cache;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.stat.CacheRegionStatistics;
import org.hibernate.stat.Statistics;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.cache.CachingRegionFactory;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
/**
* @author Christian Beikov
*/
public class CacheRegionStatisticsTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
@TestForIssue( jiraKey = "HHH-15105")
public void testAccessDefaultQueryRegionStatistics() {
final Statistics statistics = sessionFactory().getStatistics();
final CacheRegionStatistics queryRegionStatistics = statistics.getQueryRegionStatistics(
"default-query-results-region"
);
doInHibernate(
this::sessionFactory, session -> {
List<Dog> resultList = session.createQuery( "from Dog", Dog.class )
.setCacheable( true )
.getResultList();
assertEquals( 1, queryRegionStatistics.getMissCount() );
}
);
}
@Override
protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) {
super.configureStandardServiceRegistryBuilder( ssrb );
ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, true );
ssrb.applySetting( AvailableSettings.USE_QUERY_CACHE, true );
ssrb.applySetting( AvailableSettings.CACHE_REGION_FACTORY, new CachingRegionFactory() );
ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" );
}
@Override
protected void applyMetadataSources(MetadataSources metadataSources) {
super.applyMetadataSources( metadataSources );
metadataSources.addAnnotatedClass( Dog.class );
}
@Before
public void setupData() {
doInHibernate(
this::sessionFactory, session -> {
Dog yogi = new Dog( "Yogi" );
yogi.nickNames.add( "The Yog" );
yogi.nickNames.add( "Little Boy" );
yogi.nickNames.add( "Yogaroni Macaroni" );
Dog irma = new Dog( "Irma" );
irma.nickNames.add( "Squirmy" );
irma.nickNames.add( "Bird" );
session.persist( yogi );
session.persist( irma );
}
);
}
@After
public void cleanupData() {
doInHibernate(
this::sessionFactory, session -> {
List<Dog> dogs = session.createQuery( "from Dog", Dog.class ).getResultList();
for ( Dog dog : dogs ) {
session.delete( dog );
}
}
);
}
@Entity(name = "Dog")
public static class Dog {
@Id
private String name;
@ElementCollection
private Set<String> nickNames = new HashSet<>();
public Dog(String name) {
this.name = name;
}
public Dog() {
}
}
}