Commit ec165856 by ljb

解决数据库连接无响应问题

parent 0e9d716d
...@@ -29,4 +29,4 @@ public class DatabaseHolder { ...@@ -29,4 +29,4 @@ public class DatabaseHolder {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }
\ No newline at end of file
...@@ -61,4 +61,4 @@ public class DatabaseRunner implements ApplicationRunner { ...@@ -61,4 +61,4 @@ public class DatabaseRunner implements ApplicationRunner {
DatabaseHolder.set(jdbcTemplateMap); DatabaseHolder.set(jdbcTemplateMap);
log.info("成功加载数据库:{}个", jdbcTemplateMap.size()); log.info("成功加载数据库:{}个", jdbcTemplateMap.size());
} }
} }
\ No newline at end of file
...@@ -111,20 +111,15 @@ ...@@ -111,20 +111,15 @@
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.zq</groupId> <groupId>com.zq</groupId>
<artifactId>sys-server</artifactId> <artifactId>sys-server</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.zq</groupId>
<artifactId>data-collect-server</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
......
...@@ -93,12 +93,11 @@ public class CommonQueryController { ...@@ -93,12 +93,11 @@ public class CommonQueryController {
return ResultVo.success(); return ResultVo.success();
} }
// @ApiOperation("运行查询") @ApiOperation("运行查询")
// @PostMapping("/run") @PostMapping("/run")
// public ResultVo run(@RequestBody Map<String, Object> body) throws Exception { public ResultVo run(@RequestBody Map<String, Object> body) {
// System.out.println(body); return ResultVo.success(commonQueryService.run(body));
// return ResultVo.success(commonQueryService.run(body)); }
// }
@ApiOperation("自定义sql查询") @ApiOperation("自定义sql查询")
@PostMapping("/runSelect") @PostMapping("/runSelect")
......
package com.zq.dataoperation.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zq.dataoperation.entity.DataCollectTaskLog;
public interface DataCollectTaskLogMapper extends BaseMapper<DataCollectTaskLog> {
}
\ No newline at end of file
package com.zq.dataoperation.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "data_collect_task_log")
public class DataCollectTaskLog {
/**
* 数据汇聚表id
*/
private Integer dataCollectSettingId;
/**
* 开始时间
*/
private Date startTime;
/**
* 结束时间
*/
private Date endTime;
/**
* 花费多少秒
*/
private Integer duration;
/**
* 状态
* 1运行中 2正常结束 3异常终止
*/
private Integer status;
/**
* 错误信息
* 异常终止时记录异常信息
*/
private String error;
/**
* 使用序列
* id
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* createTime
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* updateTime
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
...@@ -11,30 +11,23 @@ public class DatabaseHolder { ...@@ -11,30 +11,23 @@ public class DatabaseHolder {
private static Map<Integer, JdbcTemplate> jdbcTemplateMap; private static Map<Integer, JdbcTemplate> jdbcTemplateMap;
public static Map<Integer, JdbcTemplate> getJdbcTemplateMap() {
return jdbcTemplateMap;
}
public static void set(Map<Integer, JdbcTemplate> map) { public static void set(Map<Integer, JdbcTemplate> map) {
jdbcTemplateMap = map; jdbcTemplateMap = map;
} }
public static void add(Integer id, JdbcTemplate jdbcTemplate) { public static void add(Integer id, JdbcTemplate jdbcTemplate){
if (jdbcTemplateMap == null) {
jdbcTemplateMap = new HashMap<>();
}
jdbcTemplateMap.put(id, jdbcTemplate); jdbcTemplateMap.put(id, jdbcTemplate);
} }
public static JdbcTemplate getJdbcTemplate(Integer id) { public static JdbcTemplate getJdbcTemplate(Integer id){
return jdbcTemplateMap.get(id); return jdbcTemplateMap.get(id);
} }
public static Connection getConnection(Integer id) { public static Connection getConnection(Integer id){
try { try {
return jdbcTemplateMap.get(id).getDataSource().getConnection(); return jdbcTemplateMap.get(id).getDataSource().getConnection();
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }
\ No newline at end of file
...@@ -2,32 +2,34 @@ package com.zq.dataoperation.runner; ...@@ -2,32 +2,34 @@ package com.zq.dataoperation.runner;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zq.common.exception.BusinessException; import com.zq.common.exception.BusinessException;
import com.zq.dataoperation.dao.QueryDbDao;
import com.zq.dataoperation.entity.QueryDb; import com.zq.dataoperation.entity.QueryDb;
import com.zq.dataoperation.holder.DatabaseHolder; import com.zq.dataoperation.holder.DatabaseHolder;
import com.zq.dataoperation.service.CommonQueryService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Slf4j @Slf4j
@Component @Component
public class DatabaseRunner implements ApplicationRunner { public class DatabaseRunner implements ApplicationRunner {
@Resource @Resource
private QueryDbDao queryDbDao; private CommonQueryService service;
private Map<Integer, JdbcTemplate> jdbcTemplateMap = new HashMap<>();
@Override @Override
public void run(ApplicationArguments args) throws SQLException { public void run(ApplicationArguments args) throws SQLException {
List<QueryDb> datasource = queryDbDao.selectList(new QueryWrapper<>()); List<QueryDb> datasource = service.getDatasource();
for (QueryDb ds : datasource) { for (QueryDb ds : datasource) {
DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build(); DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build();
String url = ""; String url = "";
...@@ -53,8 +55,9 @@ public class DatabaseRunner implements ApplicationRunner { ...@@ -53,8 +55,9 @@ public class DatabaseRunner implements ApplicationRunner {
druidDataSource.setUsername(ds.getUsername()); druidDataSource.setUsername(ds.getUsername());
druidDataSource.setPassword(ds.getPassword()); druidDataSource.setPassword(ds.getPassword());
druidDataSource.init(); druidDataSource.init();
DatabaseHolder.add(ds.getId(), new JdbcTemplate(druidDataSource)); jdbcTemplateMap.put(ds.getId(), new JdbcTemplate(druidDataSource));
} }
log.info("成功加载数据库:{}个", DatabaseHolder.getJdbcTemplateMap().size()); DatabaseHolder.set(jdbcTemplateMap);
log.info("成功加载数据库:{}个", jdbcTemplateMap.size());
} }
} }
\ No newline at end of file
...@@ -4,11 +4,11 @@ import cn.hutool.core.date.DateUtil; ...@@ -4,11 +4,11 @@ import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zq.common.utils.AssertUtils; import com.zq.common.utils.AssertUtils;
import com.zq.common.vo.ResultVo; import com.zq.common.vo.ResultVo;
import com.zq.datacollect.entity.DataCollectTaskLog;
import com.zq.datacollect.mapper.DataCollectTaskLogMapper;
import com.zq.dataoperation.dao.DataCleanRecordDao; import com.zq.dataoperation.dao.DataCleanRecordDao;
import com.zq.dataoperation.dao.DataCollectTaskLogMapper;
import com.zq.dataoperation.dao.QueryDbDao; import com.zq.dataoperation.dao.QueryDbDao;
import com.zq.dataoperation.entity.DataCleanRecord; import com.zq.dataoperation.entity.DataCleanRecord;
import com.zq.dataoperation.entity.DataCollectTaskLog;
import com.zq.dataoperation.entity.QueryDb; import com.zq.dataoperation.entity.QueryDb;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
......
package com.zq.dataoperation.utils;
import com.alibaba.druid.pool.DruidPooledConnection;
import java.sql.Connection;
import java.util.Map;
public class ConnectionUtil {
private static Map<Integer, DruidPooledConnection> connectionMap;
public static void set(Map<Integer, DruidPooledConnection> map) {
connectionMap = map;
}
public static Connection get(Integer id){
return connectionMap.get(id).getConnection();
}
}
...@@ -31,6 +31,7 @@ import java.sql.*; ...@@ -31,6 +31,7 @@ import java.sql.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* @author / * @author /
...@@ -234,4 +235,30 @@ public class SqlUtils { ...@@ -234,4 +235,30 @@ public class SqlUtils {
return sqlList; return sqlList;
} }
} public static String jointInsertSql(String tableName, Set<String> cloums){
StringBuilder fileds = new StringBuilder();
for (String cloum : cloums) {
fileds.append(cloum.replaceAll("\\p{Punct}", "")).append(",");
}
fileds.deleteCharAt(fileds.length()-1);
StringBuilder sql = new StringBuilder("INSERT INTO ").append(tableName).append("(").append(fileds).append(") VALUES (");
for (int i = 0; i < cloums.size(); i++) {
if(i==0){
sql.append("?");
}else {
sql.append(",?");
}
}
sql.append(")");
return sql.toString();
}
public static String joinCreateTableSql(String tableName, Set<String> cloums){
StringBuilder sql = new StringBuilder("CREATE TABLE ").append(tableName).append("(");
for (String cloum : cloums) {
sql.append(cloum.replaceAll("\\p{Punct}", "")).append(" VARCHAR(255),");
}
sql.deleteCharAt(sql.length()-1).append(")");
return sql.toString();
}
}
\ No newline at end of file
...@@ -3,7 +3,6 @@ package com.zq.dataoperation.utils; ...@@ -3,7 +3,6 @@ package com.zq.dataoperation.utils;
import com.zq.dataoperation.holder.DatabaseHolder; import com.zq.dataoperation.holder.DatabaseHolder;
import com.zq.dataoperation.vo.Column; import com.zq.dataoperation.vo.Column;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.springframework.jdbc.core.JdbcTemplate;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
...@@ -18,35 +17,49 @@ public class TableUtil { ...@@ -18,35 +17,49 @@ public class TableUtil {
@SneakyThrows @SneakyThrows
public static Object getColumns(Integer queryId, String tableName) { public static Object getColumns(Integer queryId, String tableName) {
String driver = "com.mysql.cj.jdbc.Driver"; List<Column> columns = new ArrayList<>();
Class.forName(driver); Connection connection = null;
Connection connection = DatabaseHolder.getConnection(queryId); ResultSet idxRs = null;
DatabaseMetaData metaData = connection.getMetaData(); ResultSet columnRs = null;
String primaryColumn = ""; try {
ResultSet idxRs = metaData.getIndexInfo(connection.getCatalog(), connection.getCatalog(), tableName, false, false); String driver = "com.mysql.cj.jdbc.Driver";
while (idxRs.next()) { Class.forName(driver);
String indexName = idxRs.getString("INDEX_NAME"); connection = DatabaseHolder.getConnection(queryId);
if ("PRIMARY".equals(indexName)) { DatabaseMetaData metaData = connection.getMetaData();
primaryColumn = idxRs.getString("COLUMN_NAME"); String primaryColumn = "";
break; idxRs = metaData.getIndexInfo(connection.getCatalog(), connection.getCatalog(), tableName, false, false);
while (idxRs.next()) {
String indexName = idxRs.getString("INDEX_NAME");
if ("PRIMARY".equals(indexName)) {
primaryColumn = idxRs.getString("COLUMN_NAME");
break;
}
} }
}
List<Column> columns = new ArrayList<>(); columnRs = metaData.getColumns(connection.getCatalog(), connection.getCatalog(), tableName, "%");
ResultSet columnRs = metaData.getColumns(connection.getCatalog(), connection.getCatalog(), tableName, "%"); while (columnRs.next()) {
while (columnRs.next()) { Column column = new Column();
Column column = new Column(); column.setName(columnRs.getString("COLUMN_NAME"));
column.setName(columnRs.getString("COLUMN_NAME")); column.setType(columnRs.getString("TYPE_NAME"));
column.setType(columnRs.getString("TYPE_NAME")); column.setLength(columnRs.getString("COLUMN_SIZE"));
column.setLength(columnRs.getString("COLUMN_SIZE")); column.setDecimalDigits(columnRs.getString("DECIMAL_DIGITS"));
column.setDecimalDigits(columnRs.getString("DECIMAL_DIGITS")); column.setNotNull(!columnRs.getBoolean("NULLABLE"));
column.setNotNull(!columnRs.getBoolean("NULLABLE")); column.setPrimary(column.getName().equals(primaryColumn));
column.setPrimary(column.getName().equals(primaryColumn)); column.setComment(columnRs.getString("REMARKS"));
column.setComment(columnRs.getString("REMARKS")); column.setQueryId(queryId.toString());
column.setQueryId(queryId.toString()); columns.add(column);
columns.add(column); }
}finally {
if(columnRs!=null){
columnRs.close();
}
if(idxRs!=null){
idxRs.close();
}
if(connection!=null){
connection.close();
}
} }
return columns; return columns;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment