当前位置:网站首页>監控數據源連接池使用情况
監控數據源連接池使用情况
2022-06-29 09:46: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數據源,方案二適用於所有的數據源,可以根據情况使用。
边栏推荐
- Chapter 12 signals (II) - examples of producers and consumers
- Pytorch summary learning series - data manipulation
- Pytorch Summary - sensor on GPU
- Making of simple addition calculator based on pyqt5 and QT Designer
- UE4 在viewport视口中显示3D可编辑点
- 【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)
- 长安链GO语言智能合约环境搭建及使用
- Simplicity Studio无法识别新买的JLink v9解决方法
- Research progress of target detection in the era of deep convolutional neural network
- 367. effective complete square dichotomy
猜你喜欢

Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear

A 2.5D Cancer Segmentation for MRI Images Based on U-Net

Matlab tips (21) matrix analysis -- partial least squares regression

数据处理时代,数据质量建设才是企业的生存之道

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

Pytorch Summary - Automatic gradient

3DMax 卡死、白屏、渲染死机问题总结

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images

The former security director of Uber faced fraud allegations and concealed the data leakage event
随机推荐
UE4 plug-in reports an error cannot open include file: 'modulemanager H 'resolve
云管理平台:OpenStack架构设计及详细解读
Summary of 3DMAX jamming, white screen and rendering crash
用户级线程和内核级线程
es报错NoNodeAvailableException[None of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
你知道BFD是什么吗?一文详解BFD协议原理及使用场景
股票炒股账号开户安全吗?是靠谱的吗?
ThinkPHP 6 uses mongodb
Construction and use of Changan chain go language smart contract environment
programing language
UE4 材质UV纹理不随模型缩放拉伸
Simplicity Studio无法识别新买的JLink v9解决方法
通用分页框架
数据可视化:数据可视化四象限,教你正确应用图标
In the future of Business Intelligence BI, how do you view the ai+bi model?
基于stm32标准库独立按键的多按键状态机的实现
Recursive RBAC menu level display infinite classification
长安链数据存储介绍及Mysql存储环境搭建
Data governance: Metadata Management (Part 2)
Western Polytechnic University, one of the "seven national defense schools", was attacked by overseas networks