HHH-14667 Avoid triggering the load of sequence metadata when not required

Also introduce SequenceMismatchStrategy#NONE, which allows to fully disable the checks
performed by SequenceMismatchStrategy on initialization; combining these two
changes, users have the option to fully skip loading the details about existing
sequences from the DB.
This commit is contained in:
Sanne Grinovero 2021-06-10 12:05:32 +01:00 committed by Sanne Grinovero
parent ab8c81482c
commit 5b2289e883
11 changed files with 199 additions and 79 deletions

View File

@ -275,7 +275,7 @@ Please report your mapping that causes the problem to us so we can examine the d
+
The default value is `false` which means Hibernate will use an algorithm to determine if the insert can be delayed or if the insert should be performed immediately.
`*hibernate.id.sequence.increment_size_mismatch_strategy*` (e.g. `LOG`, `FIX` or `EXCEPTION` (default value))::
`*hibernate.id.sequence.increment_size_mismatch_strategy*` (e.g. `LOG`, `FIX`, `NONE` or `EXCEPTION` (default value))::
This setting defines the `org.hibernate.id.SequenceMismatchStrategy` used when
Hibernate detects a mismatch between a sequence configuration in an entity mapping
and its database sequence object counterpart.

View File

@ -2423,8 +2423,9 @@ public interface AvailableSettings extends org.hibernate.jpa.AvailableSettings {
* and its database sequence object counterpart.
* </p>
* Possible values are {@link org.hibernate.id.SequenceMismatchStrategy#EXCEPTION},
* {@link org.hibernate.id.SequenceMismatchStrategy#LOG}, and
* {@link org.hibernate.id.SequenceMismatchStrategy#FIX}.
* {@link org.hibernate.id.SequenceMismatchStrategy#LOG},
* {@link org.hibernate.id.SequenceMismatchStrategy#FIX}
* and {@link org.hibernate.id.SequenceMismatchStrategy#NONE}.
* </p>
* The default value is given by the {@link org.hibernate.id.SequenceMismatchStrategy#EXCEPTION},
* meaning that an Exception is thrown when detecting such a conflict.

View File

@ -6,18 +6,25 @@
*/
package org.hibernate.engine.jdbc.env.internal;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.hibernate.HibernateException;
import org.hibernate.boot.model.source.internal.hbm.CommaSeparatedStringHelper;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.cursor.internal.StandardRefCursorSupport;
import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.env.spi.SQLStateType;
import org.hibernate.tool.schema.extract.spi.ExtractionContext;
import org.hibernate.tool.schema.extract.spi.SequenceInformation;
/**
@ -26,7 +33,9 @@ import org.hibernate.tool.schema.extract.spi.SequenceInformation;
* @author Steve Ebersole
*/
public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData {
private final JdbcEnvironment jdbcEnvironment;
private final JdbcConnectionAccess connectionAccess;
private final String connectionCatalogName;
private final String connectionSchemaName;
@ -39,11 +48,16 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
private final boolean supportsDataDefinitionInTransaction;
private final boolean doesDataDefinitionCauseTransactionCommit;
private final SQLStateType sqlStateType;
private final boolean jdbcMetadataAccessible;
private final List<SequenceInformation> sequenceInformationList;
//Lazily initialized: loading all sequence information upfront has been
//shown to be too slow in some cases. In this way we only load it
//when there is actual need for these details.
private List<SequenceInformation> sequenceInformationList;
private ExtractedDatabaseMetaDataImpl(
JdbcEnvironment jdbcEnvironment,
JdbcConnectionAccess connectionAccess,
String connectionCatalogName,
String connectionSchemaName,
boolean supportsRefCursors,
@ -54,8 +68,9 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
boolean supportsDataDefinitionInTransaction,
boolean doesDataDefinitionCauseTransactionCommit,
SQLStateType sqlStateType,
List<SequenceInformation> sequenceInformationList) {
boolean jdbcMetadataIsAccessible) {
this.jdbcEnvironment = jdbcEnvironment;
this.connectionAccess = connectionAccess;
this.connectionCatalogName = connectionCatalogName;
this.connectionSchemaName = connectionSchemaName;
this.supportsRefCursors = supportsRefCursors;
@ -66,7 +81,7 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
this.supportsDataDefinitionInTransaction = supportsDataDefinitionInTransaction;
this.doesDataDefinitionCauseTransactionCommit = doesDataDefinitionCauseTransactionCommit;
this.sqlStateType = sqlStateType;
this.sequenceInformationList = sequenceInformationList;
this.jdbcMetadataAccessible = jdbcMetadataIsAccessible;
}
@Override
@ -125,12 +140,26 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
}
@Override
public List<SequenceInformation> getSequenceInformationList() {
public synchronized List<SequenceInformation> getSequenceInformationList() {
if ( jdbcMetadataAccessible ) {
//Loading the sequence information can take a while on large databases,
//even minutes in some cases.
//We trigger this lazily as only certain combinations of configurations,
//mappings and used features actually trigger any use of such details.
if ( sequenceInformationList == null ) {
sequenceInformationList = sequenceInformationList();
}
return sequenceInformationList;
}
else {
return Collections.emptyList();
}
}
public static class Builder {
private final JdbcEnvironment jdbcEnvironment;
private final boolean jdbcMetadataIsAccessible;
private final JdbcConnectionAccess connectionAccess;
private String connectionSchemaName;
private String connectionCatalogName;
@ -143,10 +172,11 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
private boolean supportsDataDefinitionInTransaction;
private boolean doesDataDefinitionCauseTransactionCommit;
private SQLStateType sqlStateType;
private List<SequenceInformation> sequenceInformationList = Collections.emptyList();
public Builder(JdbcEnvironment jdbcEnvironment) {
public Builder(JdbcEnvironment jdbcEnvironment, boolean jdbcMetadataIsAccessible, JdbcConnectionAccess connectionAccess) {
this.jdbcEnvironment = jdbcEnvironment;
this.jdbcMetadataIsAccessible = jdbcMetadataIsAccessible;
this.connectionAccess = connectionAccess;
}
public Builder apply(DatabaseMetaData databaseMetaData) throws SQLException {
@ -217,14 +247,10 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
return this;
}
public Builder setSequenceInformationList(List<SequenceInformation> sequenceInformationList) {
this.sequenceInformationList = sequenceInformationList;
return this;
}
public ExtractedDatabaseMetaDataImpl build() {
return new ExtractedDatabaseMetaDataImpl(
jdbcEnvironment,
connectionAccess,
connectionCatalogName,
connectionSchemaName,
supportsRefCursors,
@ -235,8 +261,54 @@ public class ExtractedDatabaseMetaDataImpl implements ExtractedDatabaseMetaData
supportsDataDefinitionInTransaction,
doesDataDefinitionCauseTransactionCommit,
sqlStateType,
sequenceInformationList
jdbcMetadataIsAccessible
);
}
}
/**
* Get the sequence information List from the database.
*
* @return sequence information List
*/
private List<SequenceInformation> sequenceInformationList() {
final JdbcEnvironment jdbcEnvironment = this.jdbcEnvironment;
final Dialect dialect = this.jdbcEnvironment.getDialect();
Connection connection = null;
try {
connection = connectionAccess.obtainConnection();
final Connection c = connection;
Iterable<SequenceInformation> sequenceInformationIterable = dialect
.getSequenceInformationExtractor()
.extractMetadata( new ExtractionContext.EmptyExtractionContext() {
@Override
public Connection getJdbcConnection() {
return c;
}
@Override
public JdbcEnvironment getJdbcEnvironment() {
return jdbcEnvironment;
}
}
);
return StreamSupport.stream( sequenceInformationIterable.spliterator(), false )
.collect( Collectors.toList() );
}
catch (SQLException e) {
throw new HibernateException( "Could not fetch the SequenceInformation from the database", e );
}
finally {
if ( connection != null ) {
try {
connectionAccess.releaseConnection( connection );
}
catch (SQLException throwables) {
//ignored
}
}
}
}
}

View File

@ -6,13 +6,8 @@
*/
package org.hibernate.engine.jdbc.env.internal;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
@ -20,6 +15,7 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.env.spi.ExtractedDatabaseMetaData;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelperBuilder;
@ -35,8 +31,6 @@ import org.hibernate.exception.internal.SQLStateConversionDelegate;
import org.hibernate.exception.internal.StandardSQLExceptionConverter;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.tool.schema.extract.spi.ExtractionContext;
import org.hibernate.tool.schema.extract.spi.SequenceInformation;
import org.jboss.logging.Logger;
@ -64,7 +58,7 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
* @param serviceRegistry The service registry
* @param dialect The resolved dialect.
*/
public JdbcEnvironmentImpl(final ServiceRegistryImplementor serviceRegistry, Dialect dialect) {
public JdbcEnvironmentImpl(final ServiceRegistryImplementor serviceRegistry, final Dialect dialect) {
this.dialect = dialect;
final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );
@ -86,7 +80,7 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
identifierHelperBuilder.setNameQualifierSupport( nameQualifierSupport );
IdentifierHelper identifierHelper = null;
ExtractedDatabaseMetaDataImpl.Builder dbMetaDataBuilder = new ExtractedDatabaseMetaDataImpl.Builder( this );
ExtractedDatabaseMetaDataImpl.Builder dbMetaDataBuilder = new ExtractedDatabaseMetaDataImpl.Builder( this, false, null );
try {
identifierHelper = dialect.buildIdentifierHelper( identifierHelperBuilder, null );
dbMetaDataBuilder.setSupportsNamedParameters( dialect.supportsNamedParameters( null ) );
@ -150,8 +144,12 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
* Constructor form used from testing
*
* @param dialect The dialect
* @param jdbcConnectionAccess
*/
public JdbcEnvironmentImpl(DatabaseMetaData databaseMetaData, Dialect dialect) throws SQLException {
public JdbcEnvironmentImpl(
DatabaseMetaData databaseMetaData,
Dialect dialect,
JdbcConnectionAccess jdbcConnectionAccess) throws SQLException {
this.dialect = dialect;
this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, false );
@ -177,10 +175,9 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
}
this.identifierHelper = identifierHelper;
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this )
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this, true, jdbcConnectionAccess )
.apply( databaseMetaData )
.setSupportsNamedParameters( databaseMetaData.supportsNamedParameters() )
.setSequenceInformationList( sequenceInformationList( databaseMetaData.getConnection() ) )
.build();
this.currentCatalog = null;
@ -224,7 +221,8 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
public JdbcEnvironmentImpl(
ServiceRegistryImplementor serviceRegistry,
Dialect dialect,
DatabaseMetaData databaseMetaData) throws SQLException {
DatabaseMetaData databaseMetaData,
JdbcConnectionAccess jdbcConnectionAccess) throws SQLException {
this.dialect = dialect;
final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );
@ -256,11 +254,10 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
}
this.identifierHelper = identifierHelper;
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this )
this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this, true, jdbcConnectionAccess )
.apply( databaseMetaData )
.setConnectionSchemaName( determineCurrentSchemaName( databaseMetaData, serviceRegistry, dialect ) )
.setSupportsNamedParameters( dialect.supportsNamedParameters( databaseMetaData ) )
.setSequenceInformationList( sequenceInformationList( databaseMetaData.getConnection() ) )
.build();
// and that current-catalog and current-schema happen after it
@ -369,37 +366,4 @@ public class JdbcEnvironmentImpl implements JdbcEnvironment {
" Use org.hibernate.engine.jdbc.spi.TypeInfo.extractTypeInfo as alternative, or report an issue and explain." );
}
/**
* Get the sequence information List from the database.
*
* @param connection database connection
* @return sequence information List
*/
private List<SequenceInformation> sequenceInformationList(final Connection connection) {
try {
Iterable<SequenceInformation> sequenceInformationIterable = dialect
.getSequenceInformationExtractor()
.extractMetadata( new ExtractionContext.EmptyExtractionContext() {
@Override
public Connection getJdbcConnection() {
return connection;
}
@Override
public JdbcEnvironment getJdbcEnvironment() {
return JdbcEnvironmentImpl.this;
}
}
);
return StreamSupport.stream( sequenceInformationIterable.spliterator(), false )
.collect( Collectors.toList() );
}
catch (SQLException e) {
log.error( "Could not fetch the SequenceInformation from the database", e );
}
return Collections.emptyList();
}
}

View File

@ -114,7 +114,8 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
return new JdbcEnvironmentImpl(
registry,
dialect,
dbmd
dbmd,
jdbcConnectionAccess
);
}
catch (SQLException e) {

View File

@ -32,7 +32,13 @@ public enum SequenceMismatchStrategy {
* When detecting a mismatch, Hibernate tries to fix it by overriding the entity sequence mapping using the one
* found in the database.
*/
FIX;
FIX,
/**
* Don't perform any check. This is useful to speedup bootstrap as it won't query the sequences on the DB,
* at cost of not validating the sequences.
*/
NONE;
/**
* Interpret the configured SequenceMismatchStrategy value.

View File

@ -12,6 +12,7 @@ import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.boot.SchemaAutoTooling;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.QualifiedName;
@ -242,12 +243,6 @@ public class SequenceStyleGenerator
final boolean isPooledOptimizer = OptimizerFactory.isPooledOptimizer( optimizationStrategy );
if ( isPooledOptimizer && isPhysicalSequence( jdbcEnvironment, forceTableUse ) ) {
String databaseSequenceName = sequenceName.getObjectName().getText();
Long databaseIncrementValue = getSequenceIncrementValue( jdbcEnvironment, databaseSequenceName );
if ( databaseIncrementValue != null && !databaseIncrementValue.equals( (long) incrementSize ) ) {
int dbIncrementValue = databaseIncrementValue.intValue();
SequenceMismatchStrategy sequenceMismatchStrategy = configurationService.getSetting(
AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY,
@ -255,6 +250,13 @@ public class SequenceStyleGenerator
SequenceMismatchStrategy.EXCEPTION
);
if ( sequenceMismatchStrategy != SequenceMismatchStrategy.NONE && isPooledOptimizer && isPhysicalSequence( jdbcEnvironment, forceTableUse ) ) {
String databaseSequenceName = sequenceName.getObjectName().getText();
Long databaseIncrementValue = getSequenceIncrementValue( jdbcEnvironment, databaseSequenceName );
if ( databaseIncrementValue != null && !databaseIncrementValue.equals( (long) incrementSize ) ) {
int dbIncrementValue = databaseIncrementValue.intValue();
switch ( sequenceMismatchStrategy ) {
case EXCEPTION:
throw new MappingException(

View File

@ -0,0 +1,71 @@
/*
* 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.engine.jdbc.env.internal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.id.SequenceMismatchStrategy;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* Verifies that setting {@code AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY} to {@code none}
* is going to skip loading the sequence information from the database.
*/
@TestForIssue( jiraKey = "HHH-14667")
public class SkipLoadingSequenceInformationTest extends BaseCoreFunctionalTestCase {
@Override
protected void configure(Configuration configuration) {
configuration.setProperty( AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY, SequenceMismatchStrategy.NONE.name() );
configuration.setProperty( Environment.DIALECT, VetoingDialect.class.getName() );
}
@Entity(name="seqentity")
static class SequencingEntity {
@Id
@GenericGenerator(name = "pooledoptimizer", strategy = "enhanced-sequence",
parameters = {
@org.hibernate.annotations.Parameter(name = "optimizer", value = "pooled"),
@org.hibernate.annotations.Parameter(name = "initial_value", value = "1"),
@org.hibernate.annotations.Parameter(name = "increment_size", value = "2")
}
)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pooledoptimizer")
Integer id;
String name;
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{SequencingEntity.class};
}
@Test
public void test() {
// If it's able to boot, we're good.
}
public static class VetoingDialect extends H2Dialect {
@Override
public SequenceInformationExtractor getSequenceInformationExtractor() {
throw new IllegalStateException("Should really not invoke this method in this setup");
}
}
}

View File

@ -22,6 +22,7 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
@ -82,15 +83,16 @@ public class SQLServerDialectSequenceInformationTest extends BaseUnitTestCase {
ssrb.applySettings( Collections.singletonMap( AvailableSettings.URL, newUrl ) );
StandardServiceRegistry ssr = ssrb.build();
try ( Connection connection = ssr.getService( JdbcServices.class )
.getBootstrapJdbcConnectionAccess()
.obtainConnection() ) {
final JdbcConnectionAccess bootstrapJdbcConnectionAccess = ssr.getService( JdbcServices.class )
.getBootstrapJdbcConnectionAccess();
try ( Connection connection = bootstrapJdbcConnectionAccess.obtainConnection() ) {
try (Statement statement = connection.createStatement()) {
statement.execute( "CREATE SEQUENCE ITEM_SEQ START WITH 100 INCREMENT BY 10" );
}
JdbcEnvironment jdbcEnvironment = new JdbcEnvironmentImpl( connection.getMetaData(), dialect );
JdbcEnvironment jdbcEnvironment = new JdbcEnvironmentImpl( connection.getMetaData(), dialect, bootstrapJdbcConnectionAccess );
Iterable<SequenceInformation> sequenceInformations = SequenceInformationExtractorLegacyImpl.INSTANCE.extractMetadata(
new ExtractionContext.EmptyExtractionContext() {
@Override

View File

@ -62,6 +62,7 @@ public class SequenceFilterTest extends BaseUnitTestCase {
Map settings = new HashMap();
settings.putAll( Environment.getProperties() );
settings.put( AvailableSettings.DIALECT, H2Dialect.class.getName() );
settings.put( "hibernate.temp.use_jdbc_metadata_defaults", "false" );
settings.put( AvailableSettings.FORMAT_SQL, false );
this.serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( settings );

View File

@ -58,10 +58,11 @@ public class BasicTestingJdbcServiceImpl implements JdbcServices, ServiceRegistr
dialect = ConnectionProviderBuilder.getCorrespondingDialect();
connectionProvider = ConnectionProviderBuilder.buildConnectionProvider( allowAggressiveRelease );
sqlStatementLogger = new SqlStatementLogger( true, false, false );
this.jdbcConnectionAccess = new JdbcConnectionAccessImpl( connectionProvider );
Connection jdbcConnection = connectionProvider.getConnection();
try {
jdbcEnvironment = new JdbcEnvironmentImpl( jdbcConnection.getMetaData(), dialect );
jdbcEnvironment = new JdbcEnvironmentImpl( jdbcConnection.getMetaData(), dialect, jdbcConnectionAccess );
}
finally {
try {
@ -71,7 +72,6 @@ public class BasicTestingJdbcServiceImpl implements JdbcServices, ServiceRegistr
}
}
this.jdbcConnectionAccess = new JdbcConnectionAccessImpl( connectionProvider );
}
public void release() {