Commit 7e08e47b authored by Stephane Nicoll's avatar Stephane Nicoll

Clarify datasource initializer scope

This commit clarifies the scope of the datasource initializr. In
particular, it is not possible to create the schema with that facility
and let Hibernate creates additional tables.

Closes gh-9048
parent 71c15cb6
...@@ -2136,30 +2136,24 @@ is a Hibernate feature (nothing to do with Spring). ...@@ -2136,30 +2136,24 @@ is a Hibernate feature (nothing to do with Spring).
[[howto-initialize-a-database-using-spring-jdbc]] [[howto-initialize-a-database-using-spring-jdbc]]
=== Initialize a database using Spring JDBC === Initialize a database
Spring JDBC has a `DataSource` initializer feature. Spring Boot enables it by default and Spring Boot can automatically create the schema (DDL scripts) of your `DataSource` and
loads SQL from the standard locations `schema.sql` and `data.sql` (in the root of the initialize it (DML scripts): it loads SQL from the standard root classpath locations
classpath). In addition Spring Boot will load the `schema-${platform}.sql` `schema.sql` and `data.sql`, respectively. In addition Spring Boot will process the
and `data-${platform}.sql` files (if present), where `schema-${platform}.sql` and `data-${platform}.sql` files (if present), where `platform`
`platform` is the value of `spring.datasource.platform`, e.g. you might choose to set is the value of `spring.datasource.platform`. This allows you to switch to database
it to the vendor name of the database (`hsqldb`, `h2`, `oracle`, `mysql`, specific scripts if necessary, e.g. you might choose to set it to the vendor name of the
`postgresql` etc.). Spring Boot enables the fail-fast feature of the Spring JDBC database (`hsqldb`, `h2`, `oracle`, `mysql`, `postgresql` etc.).
initializer by default, so if the scripts cause exceptions the application will fail
to start. The script locations can be changed by setting `spring.datasource.schema` and Spring Boot enables the fail-fast feature of the Spring JDBC initializer by default, so if
`spring.datasource.data`, and neither location will be processed if the scripts cause exceptions the application will fail to start. You can tune that using
`spring.datasource.initialize=false`. `spring.datasource.continue-on-error`.
To disable the fail-fast you can set `spring.datasource.continue-on-error=true`. This can be NOTE: In a JPA-based app, you can choose to let Hibernate create the schema or use
useful once an application has matured and been deployed a few times, since the scripts `schema.sql` but not both. Make sure to disable `spring.jpa.hibernate.ddl-auto` if you
can act as '`poor man's migrations`' -- inserts that fail mean that the data is already chose the later.
there, so there would be no need to prevent the application from running, for instance.
You can also disable initialization by setting `spring.datasource.initialize` to `false`.
If you want to use the `schema.sql` initialization in a JPA app (with
Hibernate) then `ddl-auto=create-drop` will lead to errors if
Hibernate tries to create the same tables. To avoid those errors set
`ddl-auto` explicitly to "" (preferable) or "none". Whether or not you use
`ddl-auto=create-drop` you can always use `data.sql` to initialize new
data.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment