Skip to content

Commit 7e08e47

Browse files
committed
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
1 parent 71c15cb commit 7e08e47

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,30 +2136,24 @@ is a Hibernate feature (nothing to do with Spring).
21362136

21372137

21382138
[[howto-initialize-a-database-using-spring-jdbc]]
2139-
=== Initialize a database using Spring JDBC
2140-
Spring JDBC has a `DataSource` initializer feature. Spring Boot enables it by default and
2141-
loads SQL from the standard locations `schema.sql` and `data.sql` (in the root of the
2142-
classpath). In addition Spring Boot will load the `schema-${platform}.sql`
2143-
and `data-${platform}.sql` files (if present), where
2144-
`platform` is the value of `spring.datasource.platform`, e.g. you might choose to set
2145-
it to the vendor name of the database (`hsqldb`, `h2`, `oracle`, `mysql`,
2146-
`postgresql` etc.). Spring Boot enables the fail-fast feature of the Spring JDBC
2147-
initializer by default, so if the scripts cause exceptions the application will fail
2148-
to start. The script locations can be changed by setting `spring.datasource.schema` and
2149-
`spring.datasource.data`, and neither location will be processed if
2150-
`spring.datasource.initialize=false`.
2151-
2152-
To disable the fail-fast you can set `spring.datasource.continue-on-error=true`. This can be
2153-
useful once an application has matured and been deployed a few times, since the scripts
2154-
can act as '`poor man's migrations`' -- inserts that fail mean that the data is already
2155-
there, so there would be no need to prevent the application from running, for instance.
2156-
2157-
If you want to use the `schema.sql` initialization in a JPA app (with
2158-
Hibernate) then `ddl-auto=create-drop` will lead to errors if
2159-
Hibernate tries to create the same tables. To avoid those errors set
2160-
`ddl-auto` explicitly to "" (preferable) or "none". Whether or not you use
2161-
`ddl-auto=create-drop` you can always use `data.sql` to initialize new
2162-
data.
2139+
=== Initialize a database
2140+
Spring Boot can automatically create the schema (DDL scripts) of your `DataSource` and
2141+
initialize it (DML scripts): it loads SQL from the standard root classpath locations
2142+
`schema.sql` and `data.sql`, respectively. In addition Spring Boot will process the
2143+
`schema-${platform}.sql` and `data-${platform}.sql` files (if present), where `platform`
2144+
is the value of `spring.datasource.platform`. This allows you to switch to database
2145+
specific scripts if necessary, e.g. you might choose to set it to the vendor name of the
2146+
database (`hsqldb`, `h2`, `oracle`, `mysql`, `postgresql` etc.).
2147+
2148+
Spring Boot enables the fail-fast feature of the Spring JDBC initializer by default, so if
2149+
the scripts cause exceptions the application will fail to start. You can tune that using
2150+
`spring.datasource.continue-on-error`.
2151+
2152+
NOTE: In a JPA-based app, you can choose to let Hibernate create the schema or use
2153+
`schema.sql` but not both. Make sure to disable `spring.jpa.hibernate.ddl-auto` if you
2154+
chose the later.
2155+
2156+
You can also disable initialization by setting `spring.datasource.initialize` to `false`.
21632157

21642158

21652159

0 commit comments

Comments
 (0)