From dbb38ce67306a0d9a237024ef14a26e7f3050cae Mon Sep 17 00:00:00 2001 From: weiye Date: Mon, 23 Dec 2019 11:14:50 +0800 Subject: [PATCH] =?UTF-8?q?jdbc=E6=93=8D=E4=BD=9C=E5=88=9B=E5=BB=BAconnect?= =?UTF-8?q?ion=E6=95=B0=E9=87=8F=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/JdbcDatasourceQueryServiceImpl.java | 12 +- .../datax/admin/tool/query/BaseQueryTool.java | 128 ++++++++++-------- 2 files changed, 84 insertions(+), 56 deletions(-) diff --git a/datax-admin/src/main/java/com/wugui/datax/admin/service/impl/JdbcDatasourceQueryServiceImpl.java b/datax-admin/src/main/java/com/wugui/datax/admin/service/impl/JdbcDatasourceQueryServiceImpl.java index 0ccf5d46..edd7cbaf 100644 --- a/datax-admin/src/main/java/com/wugui/datax/admin/service/impl/JdbcDatasourceQueryServiceImpl.java +++ b/datax-admin/src/main/java/com/wugui/datax/admin/service/impl/JdbcDatasourceQueryServiceImpl.java @@ -35,7 +35,9 @@ public class JdbcDatasourceQueryServiceImpl implements JdbcDatasourceQueryServic return Lists.newArrayList(); } BaseQueryTool queryTool = QueryToolFactory.getByDbType(jdbcDatasource); - return queryTool.getTableNames(); + List tables=queryTool.getTableNames(); + BaseQueryTool.closeConnection(); + return tables; } @Override @@ -47,7 +49,9 @@ public class JdbcDatasourceQueryServiceImpl implements JdbcDatasourceQueryServic return Lists.newArrayList(); } BaseQueryTool queryTool = QueryToolFactory.getByDbType(jdbcDatasource); - return queryTool.getColumnNames(tableName); + List columnNames=queryTool.getColumnNames(tableName); + BaseQueryTool.closeConnection(); + return columnNames; } @Override @@ -59,6 +63,8 @@ public class JdbcDatasourceQueryServiceImpl implements JdbcDatasourceQueryServic return Lists.newArrayList(); } BaseQueryTool queryTool = QueryToolFactory.getByDbType(jdbcDatasource); - return queryTool.getColumnsByQuerySql(querySql); + List columns=queryTool.getColumnsByQuerySql(querySql); + BaseQueryTool.closeConnection(); + return columns; } } diff --git a/datax-admin/src/main/java/com/wugui/datax/admin/tool/query/BaseQueryTool.java b/datax-admin/src/main/java/com/wugui/datax/admin/tool/query/BaseQueryTool.java index ef59b26e..b85cb695 100644 --- a/datax-admin/src/main/java/com/wugui/datax/admin/tool/query/BaseQueryTool.java +++ b/datax-admin/src/main/java/com/wugui/datax/admin/tool/query/BaseQueryTool.java @@ -30,7 +30,9 @@ import java.util.Map; */ public abstract class BaseQueryTool implements QueryToolInterface { - protected final Logger logger = LoggerFactory.getLogger(this.getClass()); + protected static final Logger logger = LoggerFactory.getLogger(BaseQueryTool.class); + + protected static ThreadLocal connectionThreadLocal = new ThreadLocal<>(); /** * 用于获取查询语句 */ @@ -52,36 +54,52 @@ public abstract class BaseQueryTool implements QueryToolInterface { protected JobJdbcDatasource jobJdbcDatasource; - - BaseQueryTool() { - throw new UnsupportedOperationException(); - } - - /** * 构造方法 * * @param jobJdbcDatasource */ BaseQueryTool(JobJdbcDatasource jobJdbcDatasource) throws SQLException { - //这里默认使用 hikari 数据源 - HikariDataSource dataSource = new HikariDataSource(); - dataSource.setUsername(jobJdbcDatasource.getJdbcUsername()); - dataSource.setPassword(jobJdbcDatasource.getJdbcPassword()); - dataSource.setJdbcUrl(jobJdbcDatasource.getJdbcUrl()); - dataSource.setDriverClassName(jobJdbcDatasource.getJdbcDriverClass()); - //设为只读 - dataSource.setReadOnly(true); - this.jobJdbcDatasource = jobJdbcDatasource; - this.datasource = dataSource; - this.connection = this.datasource.getConnection(); + + Connection connection = connectionThreadLocal.get(); + if(null==connection){ + //这里默认使用 hikari 数据源 + HikariDataSource dataSource = new HikariDataSource(); + dataSource.setUsername(jobJdbcDatasource.getJdbcUsername()); + dataSource.setPassword(jobJdbcDatasource.getJdbcPassword()); + dataSource.setJdbcUrl(jobJdbcDatasource.getJdbcUrl()); + dataSource.setDriverClassName(jobJdbcDatasource.getJdbcDriverClass()); + dataSource.setMaximumPoolSize(1); + //设为只读 + dataSource.setReadOnly(true); + this.jobJdbcDatasource = jobJdbcDatasource; + this.datasource = dataSource; + this.connection = this.datasource.getConnection(); + connectionThreadLocal.set(this.connection); + + }else{ + this.connection=connection; + } currentDbType = JdbcUtils.getDbType(jobJdbcDatasource.getJdbcUrl(), jobJdbcDatasource.getJdbcDriverClass()); sqlBuilder = DatabaseMetaFactory.getByDbType(currentDbType); currentSchema = getSchema(); } + public static void closeConnection() { + Connection connection = connectionThreadLocal.get(); + if(null != connection){ + try { + connection.close(); + connectionThreadLocal.remove(); + } catch (SQLException e) { + logger.error("[JDBC Exception] --> " + + "Failed to close the HikariConnection, the exceprion message is:" + e.getMessage()); + } + } + } + //根据connection获取schema - protected String getSchema() { + private String getSchema() { String res = null; try { res = connection.getCatalog(); @@ -89,9 +107,11 @@ public abstract class BaseQueryTool implements QueryToolInterface { try { res = connection.getSchema(); } catch (SQLException e1) { - e1.printStackTrace(); + logger.error("[SQLException getSchema Exception] --> " + + "the exception message is:" + e1.getMessage()); } - e.printStackTrace(); + logger.error("[getSchema Exception] --> " + + "the exception message is:" + e.getMessage()); } // 如果res是null,则将用户名当作 schema if (StrUtil.isBlank(res)) { @@ -132,7 +152,6 @@ public abstract class BaseQueryTool implements QueryToolInterface { e.setIfPrimaryKey(false); } }); - return tableInfo; } @@ -145,7 +164,8 @@ public abstract class BaseQueryTool implements QueryToolInterface { try { res = JdbcUtils.executeQuery(connection, sqlQueryTableNameComment, ImmutableList.of(currentSchema, tableName)); } catch (SQLException e) { - e.printStackTrace(); + logger.error("[getTableInfo Exception] --> " + + "the exception message is:" + e.getMessage()); } return res; } @@ -158,7 +178,8 @@ public abstract class BaseQueryTool implements QueryToolInterface { try { res = JdbcUtils.executeQuery(connection, sqlQueryTables, ImmutableList.of(currentSchema)); } catch (SQLException e) { - e.printStackTrace(); + logger.error("[getTables Exception] --> " + + "the exception message is:" + e.getMessage()); } return res; } @@ -184,16 +205,14 @@ public abstract class BaseQueryTool implements QueryToolInterface { //构建 fullColumn fullColumn = buildFullColumn(dasColumns); - logger.info("fullColumn: "); - fullColumn.forEach(e -> logger.info(e.toString())); - } catch (SQLException e) { - e.printStackTrace(); + logger.error("[getColumns Exception] --> " + + "the exception message is:" + e.getMessage()); } return fullColumn; } - protected List buildFullColumn(List dasColumns) { + private List buildFullColumn(List dasColumns) { List res = Lists.newArrayList(); dasColumns.forEach(e -> { ColumnInfo columnInfo = new ColumnInfo(); @@ -206,7 +225,7 @@ public abstract class BaseQueryTool implements QueryToolInterface { } //构建DasColumn对象 - public List buildDasColumn(String tableName, ResultSetMetaData metaData) { + private List buildDasColumn(String tableName, ResultSetMetaData metaData) { List res = Lists.newArrayList(); try { int columnCount = metaData.getColumnCount(); @@ -217,7 +236,6 @@ public abstract class BaseQueryTool implements QueryToolInterface { dasColumn.setColumnName(metaData.getColumnName(i)); res.add(dasColumn); } - Statement statement = connection.createStatement(); res.forEach(e -> { String sqlQueryComment = sqlBuilder.getSQLQueryComment(currentSchema, tableName, e.getColumnName()); @@ -227,26 +245,31 @@ public abstract class BaseQueryTool implements QueryToolInterface { while (resultSetComment.next()) { e.setColumnComment(resultSetComment.getString(1)); } + JdbcUtils.close(resultSetComment); } catch (SQLException e1) { - e1.printStackTrace(); + logger.error("[buildDasColumn executeQuery Exception] --> " + + "the exception message is:" + e1.getMessage()); } }); + JdbcUtils.close(statement); } catch (SQLException e) { - e.printStackTrace(); + logger.error("[buildDasColumn Exception] --> " + + "the exception message is:" + e.getMessage()); } return res; } //获取指定表的主键,可能是多个,所以用list - public List getPrimaryKeys(String tableName) { + private List getPrimaryKeys(String tableName) { List res = Lists.newArrayList(); String sqlQueryPrimaryKey = sqlBuilder.getSQLQueryPrimaryKey(); try { List> pkColumns = JdbcUtils.executeQuery(datasource, sqlQueryPrimaryKey, ImmutableList.of(currentSchema, tableName)); //返回主键名称即可 - pkColumns.stream().forEach(e -> res.add((String) new ArrayList<>(e.values()).get(0))); + pkColumns.forEach(e -> res.add((String) new ArrayList<>(e.values()).get(0))); } catch (SQLException e) { - e.printStackTrace(); + logger.error("[getPrimaryKeys Exception] --> " + + "the exception message is:" + e.getMessage()); } return res; } @@ -256,6 +279,7 @@ public abstract class BaseQueryTool implements QueryToolInterface { List res = Lists.newArrayList(); Statement stmt = null; + ResultSet rs = null; try { //获取查询指定表所有字段的sql语句 String querySql = sqlBuilder.getSQLQueryFields(tableName); @@ -263,18 +287,18 @@ public abstract class BaseQueryTool implements QueryToolInterface { //获取所有字段 stmt = connection.createStatement(); - ResultSet resultSet = stmt.executeQuery(querySql); - ResultSetMetaData metaData = resultSet.getMetaData(); + rs = stmt.executeQuery(querySql); + ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { res.add(metaData.getColumnName(i)); } -// logger.info("res: "); -// res.forEach(e -> logger.info(e.toString())); } catch (SQLException e) { - e.printStackTrace(); + logger.error("[getColumnNames Exception] --> " + + "the exception message is:" + e.getMessage()); } finally { + JdbcUtils.close(rs); JdbcUtils.close(stmt); } return res; @@ -295,7 +319,8 @@ public abstract class BaseQueryTool implements QueryToolInterface { tables.add(tableName); } } catch (SQLException e) { - e.printStackTrace(); + logger.error("[getTableNames Exception] --> " + + "the exception message is:" + e.getMessage()); } finally { JdbcUtils.close(rs); JdbcUtils.close(stmt); @@ -317,13 +342,13 @@ public abstract class BaseQueryTool implements QueryToolInterface { List res = Lists.newArrayList(); Statement stmt = null; + ResultSet rs = null; try { - String sql = ""; //拼装sql语句,在后面加上 where 1=0 即可 - sql = querySql.concat(" where 1=0"); + String sql = querySql.concat(" where 1=0"); //判断是否已有where,如果是,则加 and 1=0 //从最后一个 ) 开始找 where,或者整个语句找 - if (querySql.indexOf(")") != -1) { + if (querySql.contains(")")) { if (querySql.substring(querySql.indexOf(")")).contains("where")) { sql = querySql.concat(" and 1=0"); } @@ -332,23 +357,20 @@ public abstract class BaseQueryTool implements QueryToolInterface { sql = querySql.concat(" and 1=0"); } } - - logger.info("querySql: {}", sql); - //获取所有字段 stmt = connection.createStatement(); - ResultSet resultSet = stmt.executeQuery(sql); - ResultSetMetaData metaData = resultSet.getMetaData(); + rs = stmt.executeQuery(sql); + ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { res.add(metaData.getColumnName(i)); } -// logger.info("res: "); -// res.forEach(e -> logger.info(e.toString())); } catch (SQLException e) { - e.printStackTrace(); + logger.error("[getColumnsByQuerySql Exception] --> " + + "the exception message is:" + e.getMessage()); } finally { + JdbcUtils.close(rs); JdbcUtils.close(stmt); } return res;