当前位置:网站首页>监控数据源连接池使用情况
监控数据源连接池使用情况
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数据源,方案二适用于所有的数据源,可以根据情况使用。
边栏推荐
- Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
- 3DMax 卡死、白屏、渲染死机问题总结
- 通识篇:原型设计的认知,设计及最佳实践
- 自定义mvc框架实现
- 第十二章 信号(二)- 生产者消费者示例
- Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
- 数据治理:数据治理在数据中台下的解决方案
- IDEA自动补全
- Iso16000-9 testing of volatile organic compounds in building products and furniture
- Recursive RBAC menu level display infinite classification
猜你喜欢

Lc236. nearest common ancestor of binary tree

User level threads and kernel level threads

UE4 在viewport视口中显示3D可编辑点

Five heart charity matchmaker team

Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer

Wechat applet user refuses to authorize geographic location information and calls up the authorization window again

The former security director of Uber faced fraud allegations and concealed the data leakage event

UE4 display 3D editable points in Viewport

1.4 机器学习方法之回归问题

Data visualization: the four quadrants of data visualization teach you to correctly apply icons
随机推荐
Wechat applet custom multi selector
Leetcode skimming -- teponacci sequence
Twinmotion 初学者教程
The former security director of Uber faced fraud allegations and concealed the data leakage event
长安链GO语言智能合约编写与编译
Construction and use of Changan chain go language smart contract environment
Print service IP setting scheme
es报错NoNodeAvailableException[None of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
Have you done the network security "physical examination" this year?
长安链GO语言智能合约环境搭建及使用
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
Wechat applet user refuses to authorize geographic location information and calls up the authorization window again
数据可视化:数据可视化四象限,教你正确应用图标
[technology development] development and design of alcohol tester solution
【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)
General part: cognition, design and best practice of prototype design
Mongodb persistence
五心公益红红娘团队
A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h
Reading notes on how to connect the network - Web server request and response (IV)