Spring Boot integrates with a number of technologies for collecting, transmitting and processing data, both SQL and NoSQL.
SQL Databases
Spring Framework provides comprehensive support for working with SQL databases, ranging from direct access to JDBC
using JdbcTemplate
to full-fledged "object-relational mapping" technologies such as Hibernate. Spring
Data provides an additional level of functionality: creating Repository
implementations directly from
interfaces and using conventions to generate queries from method names.
Configuring DataSource
The Java interface javax.sql.DataSource
provides a standard method for working with database
connections. Traditionally, a "DataSource" uses a URL
address along with some credentials to establish a
connection to the database.
Support for embedded databases
It is often more convenient to develop applications using the built-in resident database. Obviously, resident databases do not provide persistent storage. You must populate the database when the application starts and be prepared to delete data when the application exits.
Spring Boot can automatically configure embedded databases H2, HSQL and
Derby. You do not need to provide any connection URLs.
You only need to add a build dependency on the embedded database you want to use. If there are multiple embedded
databases on the class path, set the spring.datasource.embedded-database-connection
configuration
property to control which one is used. Setting the property to none
disables autoconfiguration of the
embedded database.
If you use this feature in your tests, you may notice that the same database is reused by your entire test suite,
regardless of the number of application contexts you use. If you want to ensure that each context has a separate
embedded database, you should set spring.datasource.generate-unique-name
to true
.
For example, typical POM dependencies would look like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
spring-jdbc
. In this example, it is pulled up transitively via
spring-boot-starter-data-jpa
.
DB_CLOSE_ON_EXIT=FALSE
for this. If you are using HSQLDB, you should make sure that
shutdown=true
is not used. Disabling the automatic database shutdown feature allows Spring Boot to
control when the database is closed, thereby ensuring that it occurs when access to the database is no longer
needed.
Connect to production database
Production database connections can also be automatically configured using DataSource
pooling.
DataSource configuration
DataSource configuration is controlled by external configuration properties in spring.datasource.*
. For
example, you could declare the following section in application.properties
:
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
spring.datasource.url
property. Otherwise, Spring Boot will try to automatically configure the built-in
database.
spring.datasource.driver-class-name
property.
DataSource
pool we need to make sure there is a
valid Driver
class, so we check for it before do something. In other words, if you set spring.datasource.driver-class-name=com.mysql.jdbc.Driver
,
then this class should become loadable.
For more information on supported options, see DataSourceProperties
. These are standard options that work regardless of the
actual implementation. You can also fine-tune implementation-specific parameters using the appropriate prefix
(spring.datasource.hikari.*
, spring.datasource.tomcat.*
, spring.datasource
.dbcp2.*
and spring.datasource.oracleucp.*
). For more information, see the documentation for
the connection pool implementation you are using.
For example, if you use Tomcat connection pool, You can configure many additional settings, as shown in the following example:
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.test-on-borrow=true
spring:
datasource:
tomcat:
max-wait: 10000
max-active: 50
test-on-borrow: true
This code will configure the pool to wait 10,000 ms before throwing an exception if the connection is unavailable, limit the maximum number of connections to 50, and check for a connection before borrowing it from the pool.
Supported connection pools
Spring Boot uses the following algorithm to select a specific implementation:
-
We prefer HikariCP for its performance and parallelism. If it is possible to use HikariCP, we always choose it.
-
Otherwise, if the
DataSource
pool from Tomcat is available, it is used -
If this is not available, but Commons DBCP2 is available, then use it.
-
If HikariCP, Tomcat and DBCP2 cannot be used, but Oracle UCP is available, then we use it.
spring-boot-starter-jdbc
or
spring-boot-starter-data-jpa
, then you will automatically get a dependency on HikariCP
.
You can bypass this algorithm entirely and specify the connection pool to use by setting the spring.datasource.type
property. This is especially important if the application is running in a Tomcat container, since
tomcat-jdbc
is provided by default.
Additional connection pools can always be configured manually using DataSourceBuilder
. If you define
your own DataSource
bean, there will be no automatic configuration. DataSourceBuilder
supports the following connection pools:
-
HikariCP
-
Pool
Datasource
via Tomcat -
Commons DBCP2
-
Oracle UCP and
OracleDataSource
-
SimpleDriverDataSource
from Spring Framework -
JdbcDataSource
from H2 -
PGSimpleDataSource
from PostgreSQL -
C3P0
Connecting to DataSource via JNDI
If you are deploying your Spring Boot application to an application server, you may need to configure and manage the DataSource using the application server's built-in functionality and access it via JNDI.
The spring.datasource.jndi-name
property can be used as an alternative to the spring.datasource.url
,
spring.datasource.username
and spring.datasource.password
to provide access to DataSource
from a specific location via JNDI. For example, the following section in application.properties
shows
how you can access the DataSource
defined on the JBoss AS server:
spring.datasource.jndi-name=java:jboss/datasources/customers
spring:
datasource:
jndi-name: "java:jboss/datasources/customers"