Example usage for org.hibernate.boot SchemaAutoTooling VALIDATE

List of usage examples for org.hibernate.boot SchemaAutoTooling VALIDATE

Introduction

In this page you can find the example usage for org.hibernate.boot SchemaAutoTooling VALIDATE.

Prototype

SchemaAutoTooling VALIDATE

To view the source code for org.hibernate.boot SchemaAutoTooling VALIDATE.

Click Source Link

Document

Validate the schema on SessionFactory startup.

Usage

From source file:org.grails.orm.hibernate.HibernateDatastore.java

License:Apache License

private void addTenantForSchemaInternal(String schemaName) {
    if (multiTenantMode != MultiTenancySettings.MultiTenancyMode.SCHEMA) {
        throw new ConfigurationException(
                "The method [addTenantForSchema] can only be called with multi-tenancy mode SCHEMA. Current mode is: "
                        + multiTenantMode);
    }/*from  ww  w  .  j  a v  a 2  s  . c o  m*/
    HibernateConnectionSourceFactory factory = (HibernateConnectionSourceFactory) connectionSources
            .getFactory();
    HibernateConnectionSource defaultConnectionSource = (HibernateConnectionSource) connectionSources
            .getDefaultConnectionSource();
    HibernateConnectionSourceSettings tenantSettings;
    try {
        tenantSettings = connectionSources.getDefaultConnectionSource().getSettings().clone();
    } catch (CloneNotSupportedException e) {
        throw new ConfigurationException("Couldn't clone default Hibernate settings! " + e.getMessage(), e);
    }
    tenantSettings.getHibernate().put("default_schema", schemaName);

    String dbCreate = tenantSettings.getDataSource().getDbCreate();

    SchemaAutoTooling schemaAutoTooling = dbCreate != null ? SchemaAutoTooling.interpret(dbCreate) : null;
    if (schemaAutoTooling != null && schemaAutoTooling != SchemaAutoTooling.VALIDATE
            && schemaAutoTooling != SchemaAutoTooling.NONE) {

        Connection connection = null;
        try {
            connection = defaultConnectionSource.getDataSource().getConnection();
            try {
                schemaHandler.useSchema(connection, schemaName);
            } catch (Exception e) {
                // schema doesn't exist
                schemaHandler.createSchema(connection, schemaName);
            }
        } catch (SQLException e) {
            throw new DatastoreConfigurationException(
                    String.format("Failed to create schema for name [%s]", schemaName));
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    //ignore
                }
            }
        }
    }

    DefaultConnectionSource<DataSource, DataSourceSettings> dataSourceConnectionSource = new DefaultConnectionSource<>(
            schemaName, defaultConnectionSource.getDataSource(), tenantSettings.getDataSource());
    ConnectionSource<SessionFactory, HibernateConnectionSourceSettings> connectionSource = factory
            .create(schemaName, dataSourceConnectionSource, tenantSettings);
    SingletonConnectionSources<SessionFactory, HibernateConnectionSourceSettings> singletonConnectionSources = new SingletonConnectionSources<>(
            connectionSource, connectionSources.getBaseConfiguration());
    HibernateDatastore childDatastore = new HibernateDatastore(singletonConnectionSources,
            (HibernateMappingContext) mappingContext, eventPublisher) {
        @Override
        protected HibernateGormEnhancer initialize() {
            return null;
        }
    };
    datastoresByConnectionSource.put(connectionSource.getName(), childDatastore);
}