Example usage for org.springframework.jdbc.datasource.init DataSourceInitializer setDataSource

List of usage examples for org.springframework.jdbc.datasource.init DataSourceInitializer setDataSource

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource.init DataSourceInitializer setDataSource.

Prototype

public void setDataSource(DataSource dataSource) 

Source Link

Document

The DataSource for the database to populate when this component is initialized and to clean up when this component is shut down.

Usage

From source file:org.cloudfoundry.workers.stocks.batch.BatchConfiguration.java

/**
 * We have to have certain records and certain tables for our application to
 * work correctly. This object will be used to ensure that certain
 * <CODE>sql</CODE> files are executed on application startup.
 * //w w  w .j av  a 2  s .c  o m
 * TODO make the SQL a little smarter about not recreating the tables unless
 * they don't exist.
 * 
 */
@Bean
public DataSourceInitializer dataSourceInitializer() {
    DataSourceInitializer dsi = new DataSourceInitializer();
    dsi.setDataSource(dsConfig.dataSource());
    ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
    String[] scripts = "/batch_%s.sql,/stocks_%s.sql".split(",");
    String scriptSuffix = cloudEnvironment().isCloudFoundry() ? "psql" : "h2";
    for (String s : scripts) {
        ClassPathResource classPathResource = new ClassPathResource(String.format(s, scriptSuffix));
        resourceDatabasePopulator.addScript(classPathResource);
    }
    dsi.setDatabasePopulator(resourceDatabasePopulator);
    dsi.setEnabled(true);
    return dsi;
}

From source file:org.duracloud.snapshot.db.DatabaseInitializer.java

public void init(DatabaseConfig databaseConfig) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    dataSource.setUrl(databaseConfig.getUrl());
    dataSource.setUsername(databaseConfig.getUsername());
    dataSource.setPassword(databaseConfig.getPassword());
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator(databaseConfig));

    try {// w  w w. j a  va 2s .c o m
        initializer.afterPropertiesSet();
    } catch (Exception e) {
        Throwable rootCause = getRootCause(e);

        // The database initialization SQL scripts create the necessary
        // tables.  If the exception indicates that the database already
        // contains tables then ignore the exception and continue on,
        // otherwise throw the exception.
        if (rootCause.getMessage().contains("already exists")) {
            LOGGER.info("Database initialization - tables already exist: {}", rootCause.getMessage());
        } else {
            throw e;
        }
    }
}

From source file:org.finra.dm.dao.config.DaoEnvTestSpringModuleConfig.java

/**
 * This is a data source initializer which is used to make changes to the auto-created schema based on JPA annotations and to insert reference data. This
 * bean is an InitializingBean which means it will automatically get invoked when the Spring test context creates all its beans. This approach will work for
 * making changes to the auto-created schema which got created based on other DAO beans having been created.
 *
 * @return the data source initializer./*ww w  . j  a  v  a2  s .  c o m*/
 */
@Bean
public static DataSourceInitializer dataSourceInitializer() {
    ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
    resourceDatabasePopulator.addScript(new ClassPathResource("alterJpaTablesAndInsertReferenceData.sql"));

    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    dataSourceInitializer.setDataSource(dmDataSource());
    dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
    return dataSourceInitializer;
}

From source file:org.finra.herd.dao.config.DaoEnvTestSpringModuleConfig.java

/**
 * This is a data source initializer which is used to make changes to the auto-created schema based on JPA annotations and to insert reference data. This
 * bean is an InitializingBean which means it will automatically get invoked when the Spring test context creates all its beans. This approach will work for
 * making changes to the auto-created schema which got created based on other DAO beans having been created.
 *
 * @return the data source initializer.//from   w ww  . j  a  v a2 s  .  c o  m
 */
@Bean
public static DataSourceInitializer dataSourceInitializer() {
    ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
    resourceDatabasePopulator.addScript(new ClassPathResource("alterJpaTablesAndInsertReferenceData.sql"));

    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    dataSourceInitializer.setDataSource(herdDataSource());
    dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
    return dataSourceInitializer;
}