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 application.properties:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

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
spring.datasource.url=jdbc:mysql://localhost:3306/ssb_test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
#Connection pool configuration
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.dbcp2.max-wait-millis=10000
spring.datasource.dbcp2.min-idle=5
spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.validation-query=SELECT x
spring.datasource.dbcp2.connection-properties=characterEncoding=utf8

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 http://blog.csdn.net/xiaolyuh123/article/details/73331093

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 org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DataSourceTests {

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    DataSourceProperties dataSourceProperties;

    @Test
    public void testDataSource() throws Exception {
        // Get the configured data source
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        // View Configuration Data Source Information
        System.out.println(dataSource);
        System.out.println(dataSource.getClass().getName());
        System.out.println(dataSourceProperties);
    }

}

Source code

https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases

spring-boot-student-data-jpa project

Tags: Programming Spring JDBC MySQL Tomcat

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