Spring Boot - DataSource
Configuring DataSource in a Spring Boot Project.
Config a DataSource
Add Maven dependencies for MySQL
1 | <dependency> |
configuration for MySQL datasource in application.properties file
1 | spring.datasource.url=jdbc:mysql://localhost:3306/orderDB |
That is all you need for datasource configuration. You can now autowire DataSource or JdbcTemplate in your Spring Boot app.
To test the datasource, you can list the tables using DataSource.
1 | import java.sql.DatabaseMetaData; |
H2 Datasource
When developing database application, H2 database can be used to quickly test ideas. It has a lot of benifits:
- Very fast, open source, JDBC API
- Embedded and server modes; in-memory databases
- Browser based Console application
- Small footprint: around 2 MB jar file size
You can download H2 Database from https://www.h2database.com/html/main.html page.
And then run java -jar h2-*.jar
to spin up the local server at http://127.0.1.1:8082.
To use H2 database, include the following dependencies
1 | <dependency> |
In reality, H2 dependency’s scope to be test
because it is only used in unit test unless you want to use h2 in memory database for prototyping.
By default, Spring Boot configures the application to connect to an in-memory H2 store with the username sa and an empty password. However, we can change those parameters by adding the following properties to the application.properties file
application.properties file for in memory h2 datasource. You can skip the configuration if h2 maven dependency is detected.
1 | spring.datasource.driver-class-name=org.h2.Driver |
For H2 database, the default username is “sa” and no password is required. If you are using in memory database, you actually don’t need to specify username and password at all.
application.properties file for external h2 service as datasource
1 | spring.datasource.driver-class-name=org.h2.Driver |
You need to configure connection URl for the embedded database to ensure automatic shutdown is disabled. So use DB_CLOSE_ON_EXIT=FALSE
H2 Console is a very good tool to exammine the database. it can be enabled by adding the following line to the application.properties file.
1 | spring.h2.console.enabled=true |
You can access H2 console using url http://localhost:8080/h2-console. The url and credentials are set in applicaiton.properties file.
DataSourceBuilder
When there are multiple DataSources, you will probably need to have more than one datasource set up in application.properties file. In this case, we will need to use DataSourceBuilder to manually create individual DataSource with different bean name.
application.properties file with a custom datasource
1 | spring.datasource.customsource.driver-class-name=org.h2.Driver |
Configure a Datasource using DataSourceBuilder
1 |
|
Configure a Datasource using DataSourceBuilder with the help of @ConfigurationProperties annotation
1 |
|
data.sql and schema.sql File
You can load initial data in data.sql file. When this file is presented in the classpath, spring boot will pick it up and populate the tables in the database.
If you want to use a customized schema, you can provide a customized schema.sql file under src/main/resources folder. If you do that, you need to disable JPA DDL generation.
1 | spring.jpa.hibernate.ddl-auto=none |
DDL Generation
JPA has features for DDL(Data Definition Language) generation, and these can be set up to run on startup against the database. This is controlled through two external properties:
There are two ways to set this configuration
spring.jpa.generate-ddl
- (boolean) switches the feature on and off and is vendor independent.spring.jpa.hibernate.ddl-auto
(enum) is a Hibernate feature that controls the behavior in a more fine-grained way. See below for more detail.
ddl-auto property can have one of the values: create
, create-drop
, validate
, update
and none
.
- create – Hibernate first drops existing tables, then creates new tables
- update – Hibernate attempt to update schema
- create-drop – similar to create, with the addition that Hibernate will drop the database after all operations are completed. Typically used for unit testing
- validate – Hibernate only validates whether the tables and columns exist, otherwise it throws an exception
- none – turns off the DDL generation.
In production environment, you usually use none
so that schema is not accidentially dropped.
generate-ddl will be ignored if ddl-auto is set because ddl-auto is more fine-grained.
Log SQL Statement
Add the following to application.properties file to log sql statement. format_sql
will format the sql.
Only add logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
when you need to log bind values too.
1 | spring.jpa.properties.hibernate.show_sql=true |
This will log the formatted sql statement with the bind values
1 | 2020-05-03 15:39:00.749 INFO 17557 --- [ XNIO-1 task-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 15 ms |
Reference
- Configuring a DataSource Programmatically in Spring Boot
- Spring JPA – Multiple Databases
- Spring Boot With H2 Database
- Quick Guide on Loading Initial Data with Spring Boot by Baeldung
- Stackoverflow - How to log SQL statements in Spring Boot?
- Database Initialization
Source code: SpringBootExamples - datasourcebuilder