当前位置:网站首页>Monitoring data source connection pool usage
Monitoring data source connection pool usage
2022-06-29 09:46:00 【milugloomy】
background
Last one 《 The data source connection pool is not closed Could not open JDBC Connection for transaction》 Analyze the source code MyBatis The framework and Spring Transaction management automatically turns off the data source connection pool , It solves the problem that the connection pool of online data sources is full .
But one may be solved and the next , Now you need a connection pool that can monitor the usage of the data source 、 It's almost full of alarm schemes .
Scheme 1
What we use here is Druid data source , Other data sources can use the following scheme 2 .
Druid The data source itself has a filter, After getting the data source connection ( call getConnection Method ) Will call all in turn filter.
So , We can add one when the data source is initialized filter, Used to view the current connection pool , The code is as follows :
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);
Here, when the current number of connections is greater than the maximum number of connections 80% Will print an error log , Of course, other alarm codes can be added again .
Option two
Scheme 2 can be applied to general data sources , There is no limit to Druid data source . The core idea is , Write an inherited data source subclass , Rewrite the method to get the connection , The code that implements monitoring before obtaining the connection , The sample code is as follows :
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);
}
};
// The following initializes the connection pool
dataSource.setUrl("xxx");
dataSource.setUsername("xxx");
dataSource.setPassword("xxx"));
dataSource.setDriverClassName("xxx"));
...
Conclusion
Scheme I is applicable to Druid data source , Scheme 2 is applicable to all data sources , It can be used according to the situation .
边栏推荐
- Data warehouse: layered architecture of Finance / banking
- easyexecl导出100万行execl报字体错误的解决办法
- 长安链GO语言智能合约编写与编译
- Go deep into RC, RS, daemonset and statefulset (VII)
- 3DMax 卡死、白屏、渲染死机问题总结
- UE4 材质UV纹理不随模型缩放拉伸
- Closed door cultivation (24) shallow understanding of cross domain problems
- Idea debugging fails, reporting jdwp no transports initialized, jvmtierror=agent_ ERROR_ TRANSPORT_ LOAD(196)
- KiCad学习笔记--快捷键
- Simplicity Studio无法识别新买的JLink v9解决方法
猜你喜欢

自定义mvc框架实现

Gd32f4xx Ethernet Chip (ENC28J60) Drive transplantation

UE4 material UV texture does not stretch with model scale

MySQL configuring master-slave databases

UE4 remove the mask transparent white edge in the material

Ue4 installe le plug - in datasmith dans la version 4.20-23

A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h

Kicad learning notes - shortcut keys

Visual assist plug-in settings for UE4 vs

转载 :判断对象是否具有属性的5种方法
随机推荐
GD32F4xx 以太網芯片(enc28j60)驅動移植
User level threads and kernel level threads
mysql修改自动递增初始值
用户级线程和内核级线程
Data visualization: the four quadrants of data visualization teach you to correctly apply icons
数据治理:数据治理在数据中台下的解决方案
A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)
The former security director of Uber faced fraud allegations and had concealed data leakage incidents
1.4 机器学习方法之回归问题
Reading notes on how to connect the network - Web server request and response (IV)
Pytorch summary learning series - data manipulation
The difference between cokkie and session
【NOI模拟赛】为NOI加点料(重链剖分,线段树)
通识篇:原型设计的认知,设计及最佳实践
Making of simple addition calculator based on pyqt5 and QT Designer
滑块验证代码
通用分页框架
Reading notes on how to connect the network - Web server request and response (V)
367. 有效的完全平方数-二分法