当前位置:网站首页>监控数据源连接池使用情况
监控数据源连接池使用情况
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数据源,方案二适用于所有的数据源,可以根据情况使用。
边栏推荐
- UE4 blueprint modify get a copy in array to reference
- Mac mysql数据库基本操作
- Cloud management platform: openstack architecture design and detailed interpretation
- 你知道BFD是什么吗?一文详解BFD协议原理及使用场景
- Visual assist plug-in settings for UE4 vs
- Pytorch learning summary - memory cost of operation
- Basic operations of MAC MySQL database
- Segmentation of Head and Neck Tumours Using Modified U-net
- 股票炒股账号开户安全吗?是靠谱的吗?
- programing language
猜你喜欢

UE4 display 3D editable points in Viewport

1.4 regression of machine learning methods

UE4 动画重定向

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

UE4 在4.20-23版本安装Datasmith插件

Simplicity studio does not recognize the new JLINK V9 solution

UE4 installs the datasmith plug-in in version 4.20-23

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

数据可视化:数据可视化四象限,教你正确应用图标

通用分页框架
随机推荐
滑块验证代码
After aikuai is installed or reset, the PC or mobile terminal cannot obtain IP
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
爱快安装或重置后,PC或手机端获取不到ip
你必须知道的23个最有用的Elasticseaerch检索技巧
GD32F4xx 以太網芯片(enc28j60)驅動移植
数据治理:元数据管理(第二篇)
UE4 blueprint modify get a copy in array to reference
1.4 regression of machine learning methods
LSM6DSL之SPI驱动
Official STM32 chip package download address stm32f10x stm32f40x Download
Segmentation of Head and Neck Tumours Using Modified U-net
Ue4 installe le plug - in datasmith dans la version 4.20-23
Twinmotion beginner tutorial
Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer
Easyexcl export 1million lines of EXECL report font error solution
UE4 在viewport视口中显示3D可编辑点
Research progress of target detection in the era of deep convolutional neural network
Summary of 3DMAX jamming, white screen and rendering crash
Student增删gaih