Springboot2.0以后默认数据库连接池选择了Hikari(性能高)开源地址:https://github.com/brettwooldridge/HikariCP 里面有技术文档
性能方面 hikariCP>druid>tomcat-jdbc>dbcp>c3p0 ,可以参考性能对比
https://blog.csdn.net/qq_31125793/article/details/51241943
本文介绍配置默认的hikari,对比线程池大小设置的运行结果。
上代码。
package com.figo.master.service;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.alibaba.fastjson.JSONObject;
import com.figo.master.service.impl.OrderServiceImpl;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DbConnectionPoolTest {
@Autowired
private OrderServiceImpl orderService;
int orderTime = 1000;
int time = 10;// 循环10次
long totalConsumeTime = 0;// 总耗时时间
@Test
public void dbConnPoolTest() throws Exception {
for (int i = 0; i < time; i++) {
CountDownLatch cdlTime = new CountDownLatch(orderTime);
CountDownLatch cdl = new CountDownLatch(1);
Long begin = System.currentTimeMillis();
for (int a = 0; a < orderTime; a++) {
// 生成订单信息
Thread thread = new Thread(new OrderRun(cdl,cdlTime));
thread.start();
}
cdl.countDown();
cdlTime.await();
Long end = System.currentTimeMillis();
totalConsumeTime = totalConsumeTime + (end - begin);
System.out.println(orderTime + "笔订单,总耗时=" + ((end - begin) / 1000) + "秒");
}
System.out.println(time * orderTime + "笔订单,总耗时=" + (totalConsumeTime / 1000) + "秒");
// 单元测试不支持多线程,需要sleep
Thread.sleep(300000);
}
public class OrderRun implements Runnable {
CountDownLatch cdlNew;
CountDownLatch cdlTimeNew;
public OrderRun(CountDownLatch cdl,CountDownLatch cdlTime) {
this.cdlNew = cdl;
this.cdlTimeNew=cdlTime;
}
@Override
public void run() {
try {
cdlNew.await();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JSONObject orderinfo = new JSONObject();
Long timeStamp = System.currentTimeMillis();
String uuidString = UUID.randomUUID().toString();
orderinfo.put("order_id", timeStamp + uuidString);
orderinfo.put("user_id", timeStamp + uuidString);
orderinfo.put("good_id", timeStamp + uuidString);
try {
orderService.createOrder(orderinfo);
cdlTimeNew.countDown();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
数据库写订单,跑了10次,每次并发1000笔,数据库连接池属性如下:
#datasource
spring.datasource.url=jdbc:mysql://192.160.0.12:3306/master?useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#20210718配置连接池
# Hikari will use the above plus the following to setup connection pooling
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=2
spring.datasource.hikari.maximum-pool-size=20
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
试验发现spring.datasource.hikari.maximum-pool-size=10,耗时24秒,设置为20耗时12秒,另外如果把hikari相关的属性删除,发现springboot会默认安排连接池大小,我试验下来,会安排10个左右,不会说1000个并发,就创建1000个连接,所以建议还是根据应用程序服务器和数据库服务器性能,进行hikari属性的配置。