Merge pull request #34 from dromara/feature_add_mysql

feat: 标签扩展:新增mysql查询超时设置 #18
This commit is contained in:
会编程的王学长
2022-03-17 14:57:24 +08:00
committed by GitHub
4 changed files with 22 additions and 5 deletions

View File

@@ -52,9 +52,11 @@ public class JdbcCommonCollect extends AbstractCollect {
}
JdbcProtocol jdbcProtocol = metrics.getJdbc();
String databaseUrl = constructDatabaseUrl(jdbcProtocol);
//获取查询语句/获取链接最大超时时间
int timeout = metrics.getJdbc().getTimeout().intValue();
try {
Statement statement = getConnection(jdbcProtocol.getUsername(),
jdbcProtocol.getPassword(), databaseUrl);
jdbcProtocol.getPassword(), databaseUrl,timeout);
switch (jdbcProtocol.getQueryType()) {
case QUERY_TYPE_ONE_ROW:
queryOneRow(statement, jdbcProtocol.getSql(), metrics.getAliasFields(), builder, startTime);
@@ -95,7 +97,7 @@ public class JdbcCommonCollect extends AbstractCollect {
}
private Statement getConnection(String username, String password, String url) throws Exception {
private Statement getConnection(String username, String password, String url,Integer timeout) throws Exception {
CacheIdentifier identifier = CacheIdentifier.builder()
.ip(url)
.username(username).password(password).build();
@@ -106,7 +108,7 @@ public class JdbcCommonCollect extends AbstractCollect {
try {
statement = jdbcConnect.getConnection().createStatement();
// 设置查询超时时间10秒
statement.setQueryTimeout(10);
statement.setQueryTimeout(timeout);
// 设置查询最大行数1000行
statement.setMaxRows(1000);
} catch (Exception e) {
@@ -130,7 +132,7 @@ public class JdbcCommonCollect extends AbstractCollect {
Connection connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
// 设置查询超时时间10秒
statement.setQueryTimeout(10);
statement.setQueryTimeout(timeout);
// 设置查询最大行数1000行
statement.setMaxRows(1000);
JdbcConnect jdbcConnect = new JdbcConnect(connection);

View File

@@ -35,6 +35,10 @@ public class JdbcProtocol {
* 数据库
*/
private String database;
/**
* 超时时间
*/
private Long timeout;
/**
* 数据库类型 mysql oracle ...
*/

View File

@@ -16,6 +16,10 @@ configmap:
type: 2
- key: database
type: 1
- key: timeout
type: 0
required: false
defaultValue: 3
- key: url
type: 1
# 指标组列表
@@ -61,6 +65,7 @@ metrics:
username: ^_^username^_^
password: ^_^password^_^
database: ^_^database^_^
timeout: ^_^timeout^_^
# SQL查询方式 oneRow, multiRow, columns
queryType: columns
# sql

View File

@@ -27,4 +27,10 @@ param:
- field: url
name: URL
type: text
required: false
required: false
- field: timeout
name: 超时时间
type: number
required: false
defaultValue: 3
placeholder: '请输入mysql可接受的超时时间'