diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/DatabaseInformationCachedImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/DatabaseInformationCachedImpl.java new file mode 100644 index 000000000000..8820403e5517 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/extract/internal/DatabaseInformationCachedImpl.java @@ -0,0 +1,60 @@ +/* + * 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 . + */ +package org.hibernate.tool.schema.extract.internal; + +import java.sql.SQLException; + +import org.hibernate.boot.model.relational.Namespace; +import org.hibernate.boot.model.relational.QualifiedTableName; +import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; +import org.hibernate.resource.transaction.spi.DdlTransactionIsolator; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation; +import org.hibernate.tool.schema.extract.spi.TableInformation; + +/** + * Will cache all database information by reading the tableinformation from the name space in one call. + * Schema migration becomes much faster when this object is used. + * NOTE: superclass already caches the sequence information, so perhaps cache can also be coded in superclass + * @author francois + */ +public class DatabaseInformationCachedImpl extends DatabaseInformationImpl { + private NameSpaceTablesInformation defaultNameSpaceTablesInformation; + private final Namespace defaultNamespace; + + /** + * + * @param serviceRegistry ServiceRegistry + * @param jdbcEnvironment JdbcEnvironment + * @param ddlTransactionIsolator DdlTransactionIsolator + * @param defaultNamespace NameSpace + */ + public DatabaseInformationCachedImpl( + ServiceRegistry serviceRegistry, + JdbcEnvironment jdbcEnvironment, + DdlTransactionIsolator ddlTransactionIsolator, Namespace defaultNamespace) + throws SQLException { + super(serviceRegistry, jdbcEnvironment, ddlTransactionIsolator, defaultNamespace.getName()); + this.defaultNamespace = defaultNamespace; + } + + @Override + public TableInformation getTableInformation(QualifiedTableName qualifiedTableName) { + if (defaultNameSpaceTablesInformation == null) { + // Load table information from the whole namespace in one go (expected to be used in case of schemaMigration, so (almost) all + // tables are likely to be retrieved) + defaultNameSpaceTablesInformation = getTablesInformation(defaultNamespace); + } + + TableInformation tableInformation = defaultNameSpaceTablesInformation.getTableInformation(qualifiedTableName.getTableName().getText()); + if (tableInformation != null) { + return tableInformation; + } + return super.getTableInformation(qualifiedTableName); // result of this call can of course also be cached, does not seem to be in scope now + } + +} diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java index e38b3034b076..7413fe67dde3 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java @@ -93,11 +93,7 @@ public void doMigration(Metadata metadata, ExecutionOptions options, TargetDescr final JdbcContext jdbcContext = tool.resolveJdbcContext( options.getConfigurationValues() ); final DdlTransactionIsolator ddlTransactionIsolator = tool.getDdlTransactionIsolator( jdbcContext ); try { - final DatabaseInformation databaseInformation = Helper.buildDatabaseInformation( - tool.getServiceRegistry(), - ddlTransactionIsolator, - metadata.getDatabase().getDefaultNamespace().getName() - ); + final DatabaseInformation databaseInformation = getDatabaseInformation(ddlTransactionIsolator, metadata.getDatabase().getDefaultNamespace()); final GenerationTarget[] targets = tool.buildGenerationTargets( targetDescriptor, @@ -139,6 +135,10 @@ public void doMigration(Metadata metadata, ExecutionOptions options, TargetDescr } } + protected DatabaseInformation getDatabaseInformation(DdlTransactionIsolator ddlTransactionIsolator, Namespace namespace) { + return Helper.buildDatabaseInformation(tool.getServiceRegistry(), ddlTransactionIsolator, namespace.getName()); + } + protected abstract NameSpaceTablesInformation performTablesMigration( Metadata metadata, DatabaseInformation existingDatabase, diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java index aada23b2aac0..141ed4b7b8ac 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java @@ -6,14 +6,18 @@ */ package org.hibernate.tool.schema.internal; +import java.sql.SQLException; import java.util.Set; import org.hibernate.boot.Metadata; import org.hibernate.boot.model.naming.Identifier; import org.hibernate.boot.model.relational.Namespace; import org.hibernate.dialect.Dialect; +import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.engine.jdbc.internal.Formatter; import org.hibernate.mapping.Table; +import org.hibernate.resource.transaction.spi.DdlTransactionIsolator; +import org.hibernate.tool.schema.extract.internal.DatabaseInformationCachedImpl; import org.hibernate.tool.schema.extract.spi.DatabaseInformation; import org.hibernate.tool.schema.extract.spi.NameSpaceTablesInformation; import org.hibernate.tool.schema.extract.spi.TableInformation; @@ -35,6 +39,20 @@ public GroupedSchemaMigratorImpl( super( tool, schemaFilter ); } + protected DatabaseInformation getDatabaseInformation(DdlTransactionIsolator ddlTransactionIsolator, Namespace namespace) { + final JdbcEnvironment jdbcEnvironment = tool.getServiceRegistry().getService( JdbcEnvironment.class ); + try { + return new DatabaseInformationCachedImpl( + tool.getServiceRegistry(), + jdbcEnvironment, + ddlTransactionIsolator, + namespace); + } + catch (SQLException e) { + throw jdbcEnvironment.getSqlExceptionHelper().convert( e, "Unable to build DatabaseInformationCached" ); + } + } + @Override protected NameSpaceTablesInformation performTablesMigration( Metadata metadata,