当前位置:网站首页>监控数据源连接池使用情况
监控数据源连接池使用情况
2022-06-29 08:55:00 【milugloomy】
背景
上一篇《数据源连接池未关闭的问题 Could not open JDBC Connection for transaction》分析源码了解了MyBatis框架和Spring事务管理自动关闭数据源连接池的功能,解决了一个线上数据源连接池占满的问题。
但解决了一个可能还有下一个,现在需要一个能监控数据源连接池使用情况、快占满了报警的方案。
方案一
这里使用的是Druid数据源,其他数据源可以使用下面的方案二。
Druid数据源本身有一个filter,在获取数据源连接(调用getConnection方法)时会依次调用所有的filter。
于是乎,我们就可以在数据源初始化时增加一个filter,用于查看当前连接池的情况,代码如下:
List<Filter> filters = new ArrayList<Filter>();
filters.add(new FilterAdapter(){
@Override
public DruidPooledConnection dataSource_getConnection(FilterChain chain, DruidDataSource dataSource, long maxWaitMillis) throws SQLException {
int activeCount = dataSource.getActiveCount();
if (activeCount > (dataSource.getMaxActive() * 0.8)) {
log.error("Druid Warning,the current active count of Druid Connection Pool is " + activeCount + "!");
}
return chain.dataSource_connect(dataSource, maxWaitMillis);
}
});
dataSource.setProxyFilters(filters);
这里当当前连接数大于最大连接数的80%会打印错误日志,当然也可以再次增加其他各种报警代码。
方案二
方案二可以应用在一般的数据源,不限定Druid数据源。核心思想为,写一个继承数据源子类,重写获取连接的方法,在获取连接前实现监控的代码,示例代码如下:
DruidDataSource dataSource = new DruidDataSource(){
protected Logger log = LoggerFactory.getLogger(BaqiDataSource.class);
@Override
public DruidPooledConnection getConnectionDirect(long maxWaitMillis) throws SQLException {
int activeCount = this.getActiveCount();
if (activeCount > (dataSource.getMaxActive() * 0.8)) {
log.error("Druid Warning,the current active count of Druid Connection Pool is " + activeCount + "!");
}
return super.getConnectionDirect(maxWaitMillis);
}
};
//以下初始化连接池
dataSource.setUrl("xxx");
dataSource.setUsername("xxx");
dataSource.setPassword("xxx"));
dataSource.setDriverClassName("xxx"));
...
结论
方案一适用于Druid数据源,方案二适用于所有的数据源,可以根据情况使用。
边栏推荐
- Laravel 8 enables the order table to be divided by month level
- UE4 去掉材质中Mask透明白边
- 数据治理:数据治理在数据中台下的解决方案
- Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
- MySQL configuring master-slave databases
- 安装Anaconda后启动JupyterLab需要输入密码
- GD32F4xx 以太网芯片(enc28j60)驱动移植
- 基于keil5自动配置stm32f103标准库的官网freertos移植
- 用户级线程和内核级线程
- Implementation of multi key state machine based on STM32 standard library
猜你喜欢

# 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)

Closed training (25) basic web security

Wechat applet latest canvas2d handwritten signature

Gd32f4xx Ethernet chip (ENC28J60) driver migration

UE4 蓝图修改Array 中Get a copy 为 reference

Student增删gaih

In the future of Business Intelligence BI, how do you view the ai+bi model?

MySQL configuring master-slave databases

The principle of session and cookie

SPI drive of lsm6dsl
随机推荐
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
1.4 机器学习方法之回归问题
[noi Simulation Competition] add points for noi (heavy chain dissection, line segment tree)
Student增删gaih
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
Five heart charity matchmaker team
Yotact real-time instance segmentation
How to implement observer mode
遍历vector容器中的对象的方式
Hb5470 combustion test of non-metallic materials in civil aircraft cabin
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
你必须知道的23个最有用的Elasticseaerch检索技巧
Go deep into RC, RS, daemonset and statefulset (VII)
长安链GO语言智能合约编写与编译
Data visualization: the four quadrants of data visualization teach you to correctly apply icons
Pytorch Summary - Automatic gradient
cenos7下搭建LAMP环境
云管理平台:9大开源云管理平台(CMP)
UE4 在viewport视口中显示3D可编辑点
数据治理:数据治理在数据中台下的解决方案