DataSource configuration (Spring boot DBCP2 Data source configuration) in spring boot __spring

Source: Internet
Author: User
database connection pool selection algorithm

By default, database connections can be configured automatically using the DataSource pool. Here is an algorithm for selecting a specific implementation: because of the performance and concurrency of the Tomcat data source connection pool, we always use it preferentially when Tomcat 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 the production environment. 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 gain reliance on TOMCAT-JDBC.

Note: Other connection pools can be configured manually. If you define your own datasource bean, Autoconfiguration does not occur. to configure a default data source

The datasource configuration is controlled by the Spring.datasource.* property of the external configuration file. In the example, you might declare the following fragment in the 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 the datasourceproperties. Also note that you can configure any DataSource related specific attributes through Spring.datasource.*: Refer specifically to the connection pool implementation document you are using.

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

# 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
#连接池配置
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.basicdatasource: Specifies that connection pool is used and the TOMCATE-JDBC connection pool is used by default.

DBCP2 Configuration detailed 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 Applica

    Tioncontext;

    @Autowired datasourceproperties datasourceproperties; @Test public void Testdatasource () throws Exception {//Get the configured data source DataSource DataSource = Applicationc
        Ontext.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 Engineering



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.