Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set H2 schema on connection

I am configuring an h2 test database for our oracle production database. All tables are of the schema xxx. My datasource is defined as below:

public DataSource dataSource() {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS xxx;SCHEMA=xxx");
    ds.setUser("xxx");
    ds.setPassword("xxx");
    return ds;
}

With SCHEMA=xxx, I get an error: Caused by: org.h2.jdbc.JdbcSQLException: Schema "xxx" not found; SQL statement: SET SCHEMA xxx [90079-186]

Without SCHEMA=xxx, I get errors whenever Hibernate attempts to run a query with a join because it does not prepend the schema to the table name. It does this in production with our oracle database though.

Edit: To provide some more insight, I am populating my db from creation scripts that are also used in production:

@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.setSeparator(";");
    populator.setCommentPrefix("--");
    populator.addScript(new ClassPathResource("db-schema.sql"));
    populator.addScript(new ClassPathResource("db-init-data.sql"));
    return populator;
}
like image 605
Roger Avatar asked Mar 23 '15 19:03

Roger


People also ask

How do I set the default schema in H2 database?

There is a setDefaultSchemaName() method on the Database object that you can use to set a different default schema.

How do I create a schema in H2 DB?

Example. In this example, let us create a schema named test_schema under SA user, using the following command. CREATE SCHEMA test_schema AUTHORIZATION sa; The above command produces the following output.


1 Answers

This connection string worked: ds.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS xxx\\;SET SCHEMA xxx");

like image 167
Roger Avatar answered Oct 23 '22 01:10

Roger