2

How to configure Oracle DataSource programmatically in Spring Boot with a default schema?

@Bean
public DataSource getDataSource() throws SQLException {
    OracleDataSource d = new OracleDataSource();
    d.setURL(Secrets.get("DB_URL"));
    d.setUser(Secrets.get("DB_USER"));
    d.setPassword(Secrets.get("DB_PASS"));
    // d.setSchema(System.getenv("DB_SCHEMA")); ???
    return d;
}
3
  • is it possible to use application.properties file for configuration or need manual datasource creation? Commented Jul 12, 2018 at 16:38
  • I see good property in spring configuration file reference spring.datasource.schema but, unfortunatelly, can't figure out how to apply this programmatically. (manual datasource creation needed) Commented Jul 12, 2018 at 16:43
  • ok, it is not a problem Commented Jul 12, 2018 at 16:44

4 Answers 4

3

You can't change the schema in the OracleDataSource or using connection URL, you need to execute

ALTER SESSION SET CURRENT_SCHEMA=targetschema;

statement as explained in this answer. According to Connection Properties Recognized by Oracle JDBC Drivers there is no driver property for initial schema.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Karol for answer. I know about this alter session trick but I need to find a good place to put this sql when connection initializing. Can you suggest this ?
@Daryl is it mandatory to use the OracleDataSource? Other pooling libraries have a property for this, e.g. HiakriCP offers connectionInitSql config attribute
Thank you for advice Karol. Looks like HikariDataSource does the job what I need. It has datasource wrapper method and connectionInitSql. HikariDataSource hikariDs = new HikariDataSource(); hikariDs.setDataSource(oracleDs); hikariDs.setConnectionInitSql("ALTER SESSION SET CURRENT_SCHEMA = SOME_SCHEMA"); Accepting your answer. Please update accordingly.
2

Full example:

@Bean
public DataSource getDataSource() throws SQLException {
    OracleDataSource oracleDs = new OracleDataSource();
    oracleDs.setURL(Secrets.get("DB_URL"));
    oracleDs.setUser(Secrets.get("DB_USER"));
    oracleDs.setPassword(Secrets.get("DB_PASS"));
    // other Oracle related settings...

    HikariDataSource hikariDs = new HikariDataSource();
    hikariDs.setDataSource(oracleDs);
    hikariDs.setConnectionInitSql("ALTER SESSION SET CURRENT_SCHEMA = MY_SCHEMA");

    return hikariDs;
}

1 Comment

You do not need to instantiate OracleDataSource, just HikariDataSource would do, with the appropriate settings.
1

Try to add sql execution into datasources creation method

@Bean
public DataSource getDataSource() throws SQLException {
    OracleDataSource d = new OracleDataSource();
    d.setURL(Secrets.get("DB_URL"));
    d.setUser(Secrets.get("DB_USER"));
    d.setPassword(Secrets.get("DB_PASS"));

    Resource initSchema = new ClassPathResource("scripts/schema-alter.sql");
    DatabasePopulator databasePopulator = new ResourceDatabasePopulator(initSchema);
    DatabasePopulatorUtils.execute(databasePopulator, dataSource);

    return d;
}

In scripts/schema-alter.sql will be this code

ALTER SESSION SET CURRENT_SCHEMA=targetschema;

3 Comments

Thanks Maxim for answer. HikariDataSource is more suitable for our purposes. Voting up your answer.
@Daryl oooh, i didn't know that we have ability to change dataSources implementation. I glad that answer helped to you)
Will the schema-alter.sql being run for every new connection coming from this datasource?
1

In Spring Boot 2 the wanted schema can be set in application.properties file with the following property:

spring.datasource.hikari.connection-init-sql=ALTER SESSION SET CURRENT_SCHEMA = MY_SCHEMA

HikariCP is the default connection pool in Spring Boot 2. To see all HikariCP settings (including "connectionInitSql") in you log file add also the following in application.properties:

logging.level.com.zaxxer.hikari=DEBUG

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.