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 URLaddress 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:

To auto-configure the embedded database, you must provide a dependency on spring-jdbc. In this example, it is pulled up transitively via spring-boot-starter-data-jpa.
If for some reason the connection URL for the embedded database is configured, you should ensure that the automatic database close feature is disabled. If you are using H2, then you should use 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:

    url: "jdbc:mysql://localhost/test"
    username: "dbuser"
    password: "dbpass"
You must at least specify a URL by setting the spring.datasource.url property. Otherwise, Spring Boot will try to automatically configure the built-in database.
Spring Boot can determine the JDBC driver class for most databases from a URL. If you need to specify a specific class, you can use the spring.datasource.driver-class-name property.
To create a 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:

      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:

  1. We prefer HikariCP for its performance and parallelism. If it is possible to use HikariCP, we always choose it.

  2. Otherwise, if the DataSource pool from Tomcat is available, it is used

  3. If this is not available, but Commons DBCP2 is available, then use it.

  4. If HikariCP, Tomcat and DBCP2 cannot be used, but Oracle UCP is available, then we use it.

If you are using "starter packages" 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:

    jndi-name: "java:jboss/datasources/customers"