Spring Data
Spring Boot integrates with a number of data technologies, both SQL and NoSQL.
SQL Databases
The Spring Framework provides extensive support for working with SQL databases, from direct JDBC access using jdbcTemplate
to complete "object relational mapping" technologies such as Hibernate. Spring provides an additional level of functionality.: creating Repository implementations directly from interfaces and using conventions to generate queries from your method names.
Configure a DataSource
Java's javax.sql.DataSource interface provides a standard method of working with database connections. Traditionally, a 'DataSource' uses a URL along with some credentials to establish a database connection.
Embedded Database Support
It is often convenient to develop applications by using an in-memory embedded database. Obviously, in-memory databases do not provide persistent storage. You need to populate your database when your application starts and be prepared to throw away data when your application end.
Spring Boot can auto-configure embedded H2, HSQL, and Derby databases. You need not provide any connection URLs. You need only include a build dependency to the embedded database that you want to use. If there are multiple embedded databases on the classpath, set the `spring.datasource.embedded-database-connection configuration property to control which one is used. Setting the property to none disables auto-configuration of an embedded database.
Note: If you are using this feature in your tests, you may notice that the same database is reused by your whole test suite regardless of the number of application contexts that you use. If you want to make sure that each context has a separate embedded database, you should set spring.datasource.generate-unique-name
to true
.
For example, the typical POM dependencies would be as follow:
<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>
Note: You need a dependency on spring-jdbc
for an embedded database to be auto-configured. In this example, it is pulled in transitively through spring-boot-starter-data-jpa
.
Tip: If, for whatever reason, you do configure the connection URL for an embedded database, take care to ensure that the database's automatic shutdown is disabled. If you use H2, you should use DB_CLOSE_ON_EXIT=FALSE to do so. If you use HSQLDB, you should ensure that shutdown=true is not used. Disabling the database's automatic shutdown lets Spring Boot control when the database is closed, thereby ensuring that is happens once access to the database is no longer needed.
Connection to a Production Database
Production database connections can also be auto-configured by using a pooling DataSource.
DataSource Configuration
DataSource configuration is controlled by external configuration properties in spring.datasource.*
. For example, you might declare the following section in application.properties:
spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=dbuser spring.database.password=dbpass
Note: You should at least specify the URL by setting the spring.datasource.url
property. Otherwise, Spring Boot tries to auto-configure an embedded database.
Tip: Spring Boot can deduce the JDBC driver class for most databases from the URL. If you need to specify a specific class, you can use the spring.datasource.driver-class-name
property.
Note: For a pooling DataSource to be created, we need to be able to verify that a valid Driver class is available, so we check for that before doing anything. In other words, if you set spring.datasource.driver-class-name=com.mysql.jdbc.Driver
, then that class has to be loadable.
See DataSourceProperties for more of the supported options. These are the standard options that work regardless of the actual implementation. It is also possible to fine-tune implementation-specific settings by using their respective prefix (spring.datasource.hikari.*, spring.datasource.tomcat.*, spring.datasource.dbcp2.*, and spring.datasource.oracleucp.*).
For instance, if you use the Tomcat connection pool, you could customize many additional settings, as shown in the following example:
spring: datasource: tomcat: max-wait: 10000 max-active: 50 test-on-borrow: true
This will set the pool to wait 10000ms before throwing an exception if no connection is available, limit the maximum number of connections to 50 and validate the connection before borrowing it from the pool.
Note: If you use the spring-boot--starter-jdbc or spring-boot-starter-data-jpa "starters", you automatically get a dependency to HikariCP.
You can bypass that algorithm completely and specify the connection pool to use by setting the spring.datasource.type
property. This is especially important if you run your application in a Tomcat container, as tomcat-jdbc
is provided by default.
Additional connection pools can always be configured manually, using DataSourceBuilder. If you define your own DataSource bean, auto-configuratoin does not occur. The following connection pools are supported by DataSourceBuilder:
-
HikariCP
-
Tomcat pooling
Datasource
-
Commons DBCP2
-
Oracle UCP &
OracleDataSource
-
Spring Framework’s
SimpleDriverDataSource
-
H2
JdbcDataSource
-
PostgreSQL
PGSimpleDataSource
Connection to a JNDI DataSource
If you deploy your Spring Boot application to an Application Server, you might want to configure and manage your DataSource by using your Application Server's built-in features and access it by using JNDI.
The Spring.datasource.jndi-name property can be used as an laternative to the spring.datasource.url
, spring.datasource.username
, and spring.datasource.password
properties to access the DataSource from a specific JND location. For example, the following section in application.properties show how you can access a JBoss AS defined DataSource:
spring.datasource.jndi-name=java:jboss/datasources/customers
Using JdbcTemplate
Spring's JdbcTemplate and NamedParameterJdbcTemplate classes are auto-configured, and you can @Autowire
them directly into your own beans, as shown in the following example:
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; @Component public class MyBean { private final JdbcTemplate jdbcTemplate; public MyBean(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void doSomething() { this.jdbcTemplate ... } }
You can customize some properties of the template by using the spring.jdbc.template.*
properties, as shown in the following example:
spring.jdbc.template.max-rows=500
Note: The NamedParameterJdbcTemplate
reuses the same JdbcTemplate
instance behind the scenes. If more than one JdbcTemplate
is defined and no primary candidate exists, the NamedParameterJdbcTemplate
is not auto-configured.
TBD😆