一、查看当前正在使用的连接池。
通过下面的代码,可以查看当前正在使用的连接池:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataSourceTest {
private static final Logger logger = LoggerFactory.getLogger(DataSourceTest.class);
@Autowired
private DataSource dataSource;
@Test
public void dataSourceInfo(){
logger.info(dataSource.toString());
}
}
二、默认连接池。
springboot2.1.0.RELEASE 的文档: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/
查看文档可以知道,如果我们使用spring-boot-starter-jdbc或spring-boot-starter-data-jpa “starter”,springboot将自动给我们配置连接池HikariCP。
三、hikari连接池的配置。
使用hikari连接池,可以查看对应的文档:https://github.com/brettwooldridge/HikariCP/tree/HikariCP-3.2.0
# DATASOURCE
spring.datasource.url=jdbc:mysql://IP地址/数据库名?serverTimezone=GMT%2B8
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# DATASOURCE POOL
#spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=50
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
其他属性自己查看文档配置。
参考文档:
https://blog.csdn.net/woshilijiuyi/article/details/83010997
来源:oschina
链接:https://my.oschina.net/u/2601303/blog/3063904