Example usage for org.springframework.jdbc.datasource.embedded EmbeddedDatabaseConfigurerFactory getConfigurer

List of usage examples for org.springframework.jdbc.datasource.embedded EmbeddedDatabaseConfigurerFactory getConfigurer

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource.embedded EmbeddedDatabaseConfigurerFactory getConfigurer.

Prototype

public static EmbeddedDatabaseConfigurer getConfigurer(EmbeddedDatabaseType type) throws IllegalStateException 

Source Link

Document

Return a configurer instance for the given embedded database type.

Usage

From source file:org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.java

/**
 * Set the type of embedded database to use.
 * <p>Call this when you wish to configure one of the pre-supported types.
 * <p>Defaults to HSQL./* w  w w . ja  v a 2s  .  c o  m*/
 * @param type the database type
 */
public void setDatabaseType(EmbeddedDatabaseType type) {
    this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(type);
}

From source file:org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.java

/**
 * Hook to initialize the embedded database.
 * <p>If the {@code generateUniqueDatabaseName} flag has been set to {@code true},
 * the current value of the {@linkplain #setDatabaseName database name} will
 * be overridden with an auto-generated name.
 * <p>Subclasses may call this method to force initialization; however,
 * this method should only be invoked once.
 * <p>After calling this method, {@link #getDataSource()} returns the
 * {@link DataSource} providing connectivity to the database.
 *//*from  w  w w  .j a  v a2  s. c om*/
protected void initDatabase() {
    if (this.generateUniqueDatabaseName) {
        setDatabaseName(UUID.randomUUID().toString());
    }

    // Create the embedded database first
    if (this.databaseConfigurer == null) {
        this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
    }
    this.databaseConfigurer.configureConnectionProperties(this.dataSourceFactory.getConnectionProperties(),
            this.databaseName);
    this.dataSource = this.dataSourceFactory.getDataSource();

    if (logger.isInfoEnabled()) {
        if (this.dataSource instanceof SimpleDriverDataSource) {
            SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
            logger.info(String.format("Starting embedded database: url='%s', username='%s'",
                    simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
        } else {
            logger.info(String.format("Starting embedded database '%s'", this.databaseName));
        }
    }

    // Now populate the database
    if (this.databasePopulator != null) {
        try {
            DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
        } catch (RuntimeException ex) {
            // failed to populate, so leave it as not initialized
            shutdownDatabase();
            throw ex;
        }
    }
}