Spring Boot中Datasource配置(Spring Boot dbcp2數據源配置)

數據庫鏈接池選擇算法

在默認狀況下, 數據庫鏈接可使用DataSource池進行自動配置。下面是選取一個特定實現的算法:java

  • 因爲Tomcat數據源鏈接池的性能和併發, 在tomcat可用時, 咱們老是優先使用它。
  • 若是HikariCP可用, 咱們將使用它。
  • 若是Commons DBCP可用, 咱們將使用它, 但在生產環境不推薦使用它。
  • 最後, 若是Commons DBCP2可用, 咱們將使用它。

若是你使用spring-boot-starter-jdbc或spring-boot-starter-data-jpa 'starter POMs', 你將會自動獲取對tomcat-jdbc的依賴。mysql

注:其餘的鏈接池能夠手動配置。 若是你定義本身的DataSource bean,自動配置不會發生。git

配置一個默認的數據源

DataSource配置經過外部配置文件的spring.datasource.*屬性控制。示例中,你可能會在application.properties中聲明下面的片斷:github

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

其餘可選的配置能夠查看DataSourceProperties。同時注意你能夠經過spring.datasource.*配置任何DataSource實現相關的特定屬性:具體參考你使用的鏈接池實現的文檔。web

注:既然Spring Boot可以從大多數數據庫的url上推斷出driver-class-name,那麼你就不須要再指定它了。 對於一個將要建立的DataSource鏈接池,咱們須要可以驗證Driver是否可用, 因此咱們會在作任何事情以前檢查它。 好比, 若是你設置spring.datasource.driverClassName=com.mysql.jdbc.Driver,而後這個類就會被加載。算法

配置dbcp2數據源

# 數據源配置
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:指定使用那個鏈接池,默認使用tomcate-jdbc鏈接池。spring

dbcp2配置詳解http://blog.csdn.net/xiaolyuh123/article/details/73331093sql

測試數據源代碼

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 {
        // 獲取配置的數據源
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        // 查看配置數據源信息
        System.out.println(dataSource);
        System.out.println(dataSource.getClass().getName());
        System.out.println(dataSourceProperties);
    }

}

源碼

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

spring-boot-student-data-jpa工程apache

相關文章
相關標籤/搜索