当前位置:网站首页>JDBC technology (3) - use Druid database connection pool test
JDBC technology (3) - use Druid database connection pool test
2022-08-09 01:32:00 【--meteor.】
什么是数据库连接池
数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.
一个数据库连接对象均对应一个物理数据库连接,每次操作都打开一个物理连接,使用完都关闭连接,这样造成系统的性能低下.
数据库连接池的解决方案是在应用程序启动时建立足够的数据库连接,并讲这些连接组成一个连接池(简单说:在一个“池”里放了好多半成品的数据库联接对象),由应用程序动态地对池中的连接进行申请、使用和释放.对于多于连接池中连接数的并发请求,应该在请求队列中排队等待.并且应用程序可以根据池中连接的使用率,动态增加或减少池中的连接数.
连接池技术尽可能多地重用了消耗内存地资源,大大节省了内存,提高了服务器地服务效率,能够支持更多的客户服务.通过使用连接池,将大大提高程序运行效率,同时,我们可以通过其自身的管理机制来监视数据库连接的数量、使用情况等.
前期准备
1、和JDBC技术(二)——设置通用的sql和配置文件一样
2、除此之外,Import the other twojar包
项目结构

代码编写
改动不是很大,I won't post it all
1、工具类
package com.lx.jdbc03.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/** * @author 流星 */
public class JDBCUtil3 {
static DataSource dataSource = null;
static{
try {
// Load configuration file and convert to stream,加载到properties对象中
Properties properties = new Properties();
InputStream is = JDBCUtil3.class.getClassLoader()
.getResourceAsStream("druid.properties");
properties.load(is);
// 创建连接池
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (Exception e) {
throw new RuntimeException("获取连接失败!");
}
}
public static void close(PreparedStatement preparedStatement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2、druid配置文件
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///test?useSSL=true&serverTimezone=GMT%2B8
username=root
password=123456
initialSize=5
maxActive=10
maxWait=1000
minIdle=1
测试结果

附:参考资料
Database connection pool study notes(一):原理介绍+Introduction to common connection pools
边栏推荐
- 【元胞自动机】基于元胞自动机模拟社会力因素下的灾害人员疏散应急仿真附matlab代码
- 4-6 Matplotlib库 饼图
- 知识图谱学习笔记——我的第一次知识图谱实践
- Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
- 全文翻译:EDPB数据保护影响评估(DPIA:Data Protection Impact Assessment)指南
- KQL和Lucene的区别
- 4-10 Matplotlib 多图布局
- torchversion.transforms的使用
- 【图像去噪】基于边缘增强扩散 (cEED) 和 Coherence Enhancing Diffusion (cCED) 滤波器实现图像去噪附matlab代码
- qps tps rps 区别
猜你喜欢

2022PMP项目管理认证考试报考指南(1)

4-1 Matplotlib库 数据分析常用图

EfficientNet v2网络学习记录--更小更快

Rollup 编译资源离不开 plugin

Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code

如何仿造一个websocket请求?

Mysql高级篇(逻辑架构和存储引擎)

全文翻译:欧盟第29条数据保护工作组 数据保护官指南

全文翻译:EDPB数据保护影响评估(DPIA:Data Protection Impact Assessment)指南

轻量级CNN网络高效设计准则-ShuffleNet v2学习记录
随机推荐
全文翻译:欧盟第29条数据保护工作组 数据保护官指南
论文笔记:SAITS: SELF-ATTENTION-BASED IMPUTATION FOR TIMESERIES
低代码开发创新企业应用构建模式
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
ABAP 报表中如何以二进制方式上传本地文件
如何在EasyDSS中使用ffmpeg实现点播视频的拼接与合成?
Node.js:MySQL.js的基本操作增删改查
轻量级网络SqueezeNet学习记录
观察者模式
ONNX是什么?怎么用?[简明解读版]
Wireshark packet capture tool
5-2 Seaborn 分类绘图
gstreamer 记录
A double non-programmer interviewed Ant, Meituan, Ctrip and other big companies with offers to share the interview process
ffplay播放控制
C语言-大端存储和小端存储
4-6 Matplotlib库 饼图
Introduction to LVGL (based on v8.1-8.2)
《LC刷题总结》——贪心
Pinctrl 子系统简介