0

I am switching from XML to Java based Spring configuration. Following is my xml configuration to setup and initialize my H2 database.

<beans profile="test-h2">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:target/h2/pps;AUTO_SERVER=TRUE"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>
    <bean id="entityManagerFactory" parent="entityManagerFactoryCommonParent">
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
</beans>

Following is my java based configuration to setup my H2 database in Server Mode.

private static final String H2_JDBC_URL_TEMPLATE = "jdbc:h2:%s/db/merchant;AUTO_SERVER=TRUE";
private DataSource createH2DataSource() {
    String jdbcUrl = String.format(H2_JDBC_URL_TEMPLATE, System.getProperty("user.home"));
JdbcDataSource ds = new JdbcDataSource();       
ds.setURL(jdbcUrl);
ds.setUser("sa");
ds.setPassword("");
return ds;
}

How can I run scripts to initialize schema and add in some test data? Any ideas?

2
  • if it is for tests, have a look at dbunit dbunit.sourceforge.net or DBSetup dbsetup.ninja-squad.com/index.html Commented Nov 25, 2013 at 16:08
  • I writing a single java configuration that would be useful during testing, development and production and would be able to switch between database (h2, oracle, mysql, etc) during execution based on the passed parameter. Commented Nov 25, 2013 at 16:18

1 Answer 1

1

I have found a way to do it.

@Value("classpath:seed-data.sql")
private Resource H2_SCHEMA_SCRIPT;

@Value("classpath:test-data.sql")
private Resource H2_DATA_SCRIPT;

@Value("classpath:drop-data.sql")
private Resource H2_CLEANER_SCRIPT;


@Bean
public DataSource dataSource(Environment env) throws Exception {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();          
}


@Autowired
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {

    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}


private DatabasePopulator databasePopulator() {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(H2_SCHEMA_SCRIPT);
    populator.addScript(H2_DATA_SCRIPT);
    return populator;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.