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

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

Introduction

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

Prototype

@Override
public void afterPropertiesSet() 

Source Link

Document

Use the #setDatabasePopulator database populator to set up the database.

Usage

From source file:ctv.stageissue.Application.java

@Bean
public DataSource dataSource() {
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
    config.setConnectionTestQuery("VALUES 1");
    config.addDataSourceProperty("url", "jdbc:h2:~/test");
    config.addDataSourceProperty("user", "sa");
    config.addDataSourceProperty("password", "");
    HikariDataSource dataSource = new HikariDataSource(config);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(//from  www.j  ava2s. c o m
            new ResourceDatabasePopulator(false, false, null, new ClassPathResource("model.sql")));
    initializer.setEnabled(true);
    initializer.afterPropertiesSet();

    return dataSource;
}

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  v  a 2  s . co  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;
        }
    }
}