0

The in memory database in spring boot configuration is working fine, with below code. But the url is not picking up from from YAML configuration. though the h2 console is enabled

In log the url is printing as memory db.

  1. how can i change the jdbc url from memory to file.

Log File

2020-09-03 14:22:42.595 [RMI TCP Connection(2)-10.44.33.51] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource

2020-09-03 14:22:42.595 [RMI TCP Connection(2)-10.44.33.51] DEBUG o.s.j.d.SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:testdb;MODE=Oracle;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false]

@Configuration
public class DataSourceConfiguration {

@Bean
@Primary
public DataSource dataSource() {
    return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .setName("testdb;MODE=Oracle")
            .addScript("classpath:sql/Create-table.sql")
            .build();
}
}

YAML Configuration

  spring:
      h2:
        console:
          enabled: true
          path: /h2
      datasource:
        url: jdbc:h2:file:C:/db/test
        username: test
        password: password
        driverClassName: org.h2.Driver
2
  • 1
    It might be because your overriding the DataSource bean. Commented Sep 3, 2020 at 13:24
  • yeah, I want to run script on start up , so overrided datasource bean. At the same time i want to pass url too Commented Sep 4, 2020 at 5:00

1 Answer 1

0

Don't define dataSource bean. Configure datasource via properties file. If you want to execute DDL and/or DML sql script when app starts you can do something like this:

spring:
  datasource:
    url: jdbc:h2:file:C:/db/test
    username: test
    password: password
    schema: classpath:sql/Create-table.sql
    data: classpath:sql/fill-table.sql

By default Spring Boot looking for files schema.sql and data.sql in the root of app's classpath (e.g. src/main/resources/schema.sql) but you can override this behavior in properties spiring.datasource.schema and spring.datasource.data just like in code example above.

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.