Example usage for org.springframework.jdbc.datasource.init DatabasePopulatorUtils execute

List of usage examples for org.springframework.jdbc.datasource.init DatabasePopulatorUtils execute

Introduction

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

Prototype

public static void execute(DatabasePopulator populator, DataSource dataSource) throws DataAccessException 

Source Link

Document

Execute the given DatabasePopulator against the given DataSource .

Usage

From source file:org.camelcookbook.transactions.util.DataSourceInitializer.java

public static DataSource initializeDataSource(DataSource dataSource, Resource script) {
    // here we use the same classes that Spring does under the covers to run the schema into the database
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(script);/*from   ww w  . jav  a2s  .  co  m*/
    DatabasePopulatorUtils.execute(populator, dataSource);

    return dataSource;
}

From source file:org.cloudfoundry.samples.handson.ex5.Ex5Config.java

public void databasePopulator() {
    try {//from   w  ww  .j ava  2 s. com
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(new ClassPathResource("schema.sql", Ex5Config.class));
        DatabasePopulatorUtils.execute(populator, fromDataSource());
    } catch (DataAccessResourceFailureException e) {
        //ignore if table already exists
    }
}

From source file:org.dawnsci.marketplace.config.DatabaseConfiguration.java

@Bean(name = "dataSource")
public DataSource getDataSource() {
    DataSource dataSource = createDataSource();
    DatabasePopulatorUtils.execute(createDatabasePopulator(), dataSource);
    return dataSource;
}

From source file:ch.algotrader.hibernate.InMemoryDBTest.java

@Before
public void setup() throws Exception {

    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ClassPathResource("/db/h2/h2.sql"));
    DatabasePopulatorUtils.execute(dbPopulator, EmbeddedTestDB.DATABASE.getDataSource());

    this.sessionFactory = EmbeddedTestDB.DATABASE.getSessionFactory();

    this.session = this.sessionFactory.openSession();

    TransactionSynchronizationManager.bindResource(this.sessionFactory, new SessionHolder(this.session));
}

From source file:net.gplatform.spring.social.base.JdbcUsersConnectionRepositoryTableCreator.java

public void createTableIfNotExist() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    try {// ww w. jav a 2 s  .  com
        jdbcTemplate.queryForList("select count(*) from UserConnection");
    } catch (Exception e) {
        LOG.debug("Create table UserConnection");
        try {
            ResourceDatabasePopulator rdp = new ResourceDatabasePopulator();
            rdp.addScript(new ClassPathResource(
                    "/org/springframework/social/connect/jdbc/JdbcUsersConnectionRepository.sql"));
            DatabasePopulatorUtils.execute(rdp, dataSource);
        } catch (Exception e1) {
            LOG.error("Error create table UserConnection", e1);
        }
    }
}

From source file:ch.algotrader.hibernate.InMemoryDBTest.java

@After
public void cleanup() throws Exception {

    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ByteArrayResource("DROP ALL OBJECTS".getBytes(Charsets.US_ASCII)));

    DatabasePopulatorUtils.execute(dbPopulator, EmbeddedTestDB.DATABASE.getDataSource());

    if (this.sessionFactory != null) {

        TransactionSynchronizationManager.unbindResource(EmbeddedTestDB.DATABASE.getSessionFactory());
    }/*from   w  w  w  .j a  va2 s.c o m*/
    if (this.session != null) {

        if (this.session.isOpen()) {
            this.session.close();
        }
    }
}

From source file:com.musala.configuration.RssAplicationConfiguration.java

@Bean(name = "dataSource")
public DataSource dataSource() throws SQLException {
    DataSource dataSource = createDataSource();
    DatabasePopulatorUtils.execute(createDatabasePopulator(), dataSource);
    System.out.println(dataSource.getConnection());
    return dataSource;
}

From source file:com.example.DBConfig.java

@PostConstruct
protected void initialize() throws Exception {
    String platform = DatabaseType.fromMetaData(this.dataSource()).toString().toLowerCase();
    String schemaCreateLocation = this.env.getProperty("schema", DEFAULT_SCHEMA_LOCATION);
    schemaCreateLocation = schemaCreateLocation.replace("@@platform@@", platform);
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(this.resourceLoader.getResource(schemaCreateLocation));
    populator.setContinueOnError(true);/*from   w ww .j  av a2s  .c  o m*/
    DatabasePopulatorUtils.execute(populator, dataSource());
}

From source file:util.SampleDataProvider.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

    if (!event.getApplicationContext().equals(applicationContext)) {
        return;/*from  w  w  w  . j  a v a  2 s. c  om*/
    }

    LOG.info("Populating datasource using SQL file {}!", SQL_FILE);
    if (!called) {
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.setScripts(new Resource[] { new ClassPathResource(SQL_FILE) });
        DatabasePopulatorUtils.execute(populator, dataSource);
        called = true;
    }
}

From source file:am.ik.ws.colordic.app.bootstrap.SampleDataProvider.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

    if (!event.getApplicationContext().equals(applicationContext)) {
        return;/*  www .  j  a  va  2 s. c o  m*/
    }

    LOG.info("Populating datasource using SQL file {}!", SQL_FILE);

    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setScripts(new Resource[] { new ClassPathResource(SQL_FILE) });
    DatabasePopulatorUtils.execute(populator, dataSource);
}