HHH-14948 Adapt contributed patch to 6.0 branch

This commit is contained in:
Ivaylo Mitrev 2021-12-02 17:38:26 +02:00 committed by Sanne Grinovero
parent 1172943252
commit b9814f5cef
3 changed files with 132 additions and 14 deletions

View File

@ -323,28 +323,31 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
return importInfo == null ? null : importInfo.importedName;
}
private <T> ImportInfo<T> resolveImport(String name) {
//noinspection unchecked
final ImportInfo<T> result = (ImportInfo<T>) nameToImportMap.computeIfAbsent( name, unknownName -> {
private <T> ImportInfo<T> resolveImport(final String name) {
final ImportInfo<?> importInfo = nameToImportMap.get( name );
if ( importInfo != null ) {
return importInfo == INVALID_IMPORT ? null : (ImportInfo<T>) importInfo;
}
else {
// see if the name is a fully-qualified class name
final Class<T> loadedClass = resolveRequestedClass( unknownName );
final Class<T> loadedClass = resolveRequestedClass( name );
if ( loadedClass == null ) {
// it is NOT a fully-qualified class name - add a marker entry
// so we do not keep trying later
// it is NOT a fully-qualified class name - add a marker entry so we do not keep trying later
// note that ConcurrentHashMap does not support null value so a marker entry is needed
return INVALID_IMPORT;
// [HHH-14948] But only add it if the cache size isn't getting too large, as in some use cases
// the queries are dynamically generated and this cache could lead to memory leaks when left unbounded.
if ( nameToImportMap.size() < 1_000 ) {
nameToImportMap.put( name, INVALID_IMPORT );
}
return null;
}
else {
// it is a fully-qualified class name - add it to the cache
// so we do not keep trying later
return new ImportInfo<>( unknownName, loadedClass );
final ImportInfo<T> info = new ImportInfo<>( name, loadedClass );
nameToImportMap.put( name, info );
return info;
}
} );
if ( result == INVALID_IMPORT ) {
return null;
}
else {
return result;
}
}

View File

@ -0,0 +1,86 @@
/*
* 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.orm.test.hql;
import java.lang.reflect.Field;
import java.util.Map;
import org.hibernate.metamodel.MappingMetamodel;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl;
import org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.junit.Assert.assertEquals;
public class MetamodelBoundedCacheTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-14948")
public void testMemoryConsumptionOfFailedImportsCache() throws NoSuchFieldException, IllegalAccessException {
MappingMetamodel mappingMetamodel = sessionFactory().getMetamodel();
MappingMetamodelImpl mImpl = (MappingMetamodelImpl) mappingMetamodel;
final JpaMetamodel jpaMetamodel = mImpl.getJpaMetamodel();
for ( int i = 0; i < 1001; i++ ) {
jpaMetamodel.qualifyImportableName( "nonexistend" + i );
}
Field field = JpaMetamodelImpl.class.getDeclaredField( "nameToImportMap" );
field.setAccessible( true );
//noinspection unchecked
Map<String, String> imports = (Map<String, String>) field.get( jpaMetamodel );
// VERY hard-coded, but considering the possibility of a regression of a memory-related issue,
// it should be worth it
assertEquals( 1000, imports.size() );
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Employee.class };
}
@Entity( name = "Employee" )
@Table( name= "tabEmployees" )
public class Employee {
@Id
private long id;
private String name;
public Employee() {
}
public Employee(long id, String strName) {
this();
this.name = strName;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String strName) {
this.name = strName;
}
}
}

View File

@ -13,6 +13,12 @@ import jakarta.persistence.Table;
import org.hibernate.query.hql.internal.QuerySplitter;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.stream.IntStream;
import org.hibernate.metamodel.internal.MetamodelImpl;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
@ -48,6 +54,29 @@ public class QuerySplitterTest extends BaseNonConfigCoreFunctionalTestCase {
);
}
@Test
@TestForIssue(jiraKey = "HHH-14948")
public void testMemoryConsumptionOfFailedImportsCache() throws NoSuchFieldException, IllegalAccessException {
IntStream.range( 0, 1001 )
.forEach( i -> QuerySplitter.concreteQueries(
"from Employee e join e.company" + i,
sessionFactory()
) );
MetamodelImpl metamodel = (MetamodelImpl) sessionFactory().getMetamodel();
Field field = MetamodelImpl.class.getDeclaredField( "imports" );
field.setAccessible( true );
//noinspection unchecked
Map<String, String> imports = (Map<String, String>) field.get( metamodel );
// VERY hard-coded, but considering the possibility of a regression of a memory-related issue,
// it should be worth it
assertEquals( 1000, imports.size() );
}
@Test
@TestForIssue(jiraKey = "HHH-7973" )
public void testQueryWithEntityNameAsStringLiteralAndEscapeQuoteChar() {