Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run SQL scripts and get data on application startup?

I am developing a Spring Boot application. At the moment some of my configs are hard coded (e.g. Hystrix properties).

So I would like to get these configs on my application start up time or just after that.

Is it possible to do that using Spring Boot? I mean to run SQL script on start up and get data.

How should properties/configs be retrieved and stored in my application?

I am using MyBatis and Oracle DB.

like image 408
Laurynas Avatar asked Sep 01 '16 20:09

Laurynas


People also ask

Where do I put SQL data in spring boot?

Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema. sql and data. sql , respectively.


2 Answers

By default, Spring-Boot loads data.sql and/or data-${platform}.sql.

However, keep in mind that the script would be loaded at every start, so I would think it makes more sense (at least for production), to just have the values already present in the database, not re-inserted at every start. I've personally only used database initialization for test/dev purposes when using a memory database. Still, this is the feature provided by Spring-Boot.

source: spring-boot-howto-database-initialization:

Spring JDBC has a DataSource initializer feature. Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql (in the root of the classpath). In addition Spring Boot will load the schema-${platform}.sql and data-${platform}.sql files (if present).

src/main/resources/data-oracle.sql:

insert into... insert into... 
  • You may define the platform with: spring.datasource.platform=oracle.
  • You may change the name of the sql script to load with: spring.datasource.data=myscript.sql.
  • Along with data.sql, Spring-boot also loads schema.sql (before data.sql).
  • You could also have an "update or insert" logic in your data.sql: oracle sql: update if exists else insert
like image 185
alexbt Avatar answered Sep 18 '22 17:09

alexbt


What worked for me is using DataSourceInitializer:

@Bean public DataSourceInitializer dataSourceInitializer(@Qualifier("dataSource") final DataSource dataSource) {     ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();     resourceDatabasePopulator.addScript(new ClassPathResource("/data.sql"));     DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();     dataSourceInitializer.setDataSource(dataSource);     dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);     return dataSourceInitializer; } 

Used to set up a database during initialization and clean up a database during destruction.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/datasource/init/DataSourceInitializer.html

like image 39
Emerica Avatar answered Sep 18 '22 17:09

Emerica