Data source configuration in Spring Boot (Spring Boot dbcp2 data source configuration)

Database Connection Pool Selection Algorithms

By default, database connections can be automatically configured using the DataSource pool. The following is an algorithm for selecting a specific implementation:

  • Because of the performance and concurrency of Tomcat's data source connection pool, we always give preference to Tomcat when it is available.
  • If HikariCP is available, we will use it.
  • If Commons DBCP is available, we will use it, but it is not recommended in production environments.
  • Finally, if Commons DBCP2 is available, we will use it.

If you use spring-boot-starter-jdbc or spring-boot-starter-data-jpa'starter POMs', you will automatically acquire dependencies on tomcat-jdbc.

Note: Other connection pools can be configured manually. If you define your own DataSource bean, automatic configuration will not happen.

Configure a default data source

The DataSource configuration is controlled by the spring.datasource. * attribute of the external configuration file. In the example, you might declare the following fragment in


Other optional configurations allow you to view DataSource Properties. Also note that you can configure any DataSource with spring.datasource. * to implement specific attributes: refer specifically to the connection pool documentation you use.

Note: Since Spring Book can infer driver-class-name from the URLs of most databases, you don't need to specify it anymore. For a DataSource connection pool to be created, we need to be able to verify that the Driver is available, so we will check it before doing anything. For example, if you set spring.datasource.driverClassName=com.mysql.jdbc.Driver, then the class will be loaded.

Configure dbcp2 data source

# Data source configuration
#Connection pool configuration
spring.datasource.dbcp2.validation-query=SELECT x

Spring. datasource. type = org. apache. commons. dbcp2. Basic DataSource: Specifies which connection pool to use, and defaults to tomcate-jdbc connection pool.

Detailed description of dbcp2 configuration

Test data source code

package com.xiaolyuh;

import net.minidev.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

import static;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class DataSourceTests {

    ApplicationContext applicationContext;

    DataSourceProperties dataSourceProperties;

    public void testDataSource() throws Exception {
        // Get the configured data source
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        // View Configuration Data Source Information


Source code

spring-boot-student-data-jpa project

Tags: Programming Spring JDBC MySQL Tomcat

Posted on Sat, 21 Sep 2019 03:08:51 -0700 by bashaash