优化数据源初始化和获取逻辑
This commit is contained in:
@@ -114,7 +114,7 @@ public class DatabaseDocController {
|
||||
return DocDbResponseJson.ok(resultObj);
|
||||
}
|
||||
BaseMapper baseMapper = this.getBaseMapper(sourceId);
|
||||
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getFactoryById(sourceId);
|
||||
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getOrCreateFactoryById(sourceId);
|
||||
List<DatabaseInfoDto> dbNameDtoList = baseMapper.getDatabaseList();
|
||||
Map<String, List<TableInfoDto>> dbTableMap = new HashMap<>();
|
||||
Map<String, List<TableColumnDescDto>> tableColumnsMap = new HashMap<>();
|
||||
@@ -162,7 +162,7 @@ public class DatabaseDocController {
|
||||
public ResponseJson getTableDdl(Long sourceId, String dbName, String tableName) {
|
||||
BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
|
||||
Map<String, String> dataMap = baseMapper.getTableDdl(dbName, tableName);
|
||||
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getFactoryById(sourceId);
|
||||
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getOrCreateFactoryById(sourceId);
|
||||
// 不同数据源类型获取方式不一致
|
||||
if (Objects.equals(DatabaseProduct.MYSQL, databaseFactoryBean.getDatabaseProduct())) {
|
||||
return DocDbResponseJson.ok(dataMap.get("Create Table"));
|
||||
@@ -181,7 +181,7 @@ public class DatabaseDocController {
|
||||
public ResponseJson getTableStatus(Long sourceId, String dbName, String tableName) {
|
||||
BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
|
||||
TableStatusVo tableStatusVo = baseMapper.getTableStatus(dbName, tableName);
|
||||
DatabaseFactoryBean factoryBean = databaseRegistrationBean.getFactoryById(sourceId);
|
||||
DatabaseFactoryBean factoryBean = databaseRegistrationBean.getOrCreateFactoryById(sourceId);
|
||||
tableStatusVo.setDbType(factoryBean.getDatabaseProduct().name().toLowerCase());
|
||||
return DocDbResponseJson.ok(tableStatusVo);
|
||||
}
|
||||
@@ -196,7 +196,7 @@ public class DatabaseDocController {
|
||||
@PostMapping(value = "/getTableColumnList")
|
||||
public ResponseJson getTableColumnList(Long sourceId, String dbName, String tableName) {
|
||||
this.judgeAuth(sourceId, DbAuthType.VIEW.getName(), "没有查看该库表信息的权限");
|
||||
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getFactoryById(sourceId);
|
||||
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getOrCreateFactoryById(sourceId);
|
||||
if (databaseFactoryBean == null) {
|
||||
return DocDbResponseJson.warn("未找到对应的数据库连接");
|
||||
}
|
||||
@@ -267,7 +267,7 @@ public class DatabaseDocController {
|
||||
if (StringUtils.isBlank(tableNames)) {
|
||||
return DocDbResponseJson.warn("请选择需要导出的表");
|
||||
}
|
||||
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getFactoryById(sourceId);
|
||||
DatabaseFactoryBean databaseFactoryBean = databaseRegistrationBean.getOrCreateFactoryById(sourceId);
|
||||
if (databaseFactoryBean == null) {
|
||||
return DocDbResponseJson.warn("未找到对应的数据库连接");
|
||||
}
|
||||
|
||||
@@ -77,31 +77,17 @@ public class DbDatasourceController {
|
||||
dbDatasource.setYn(1);
|
||||
dbDatasourceService.save(dbDatasource);
|
||||
}
|
||||
// 关闭数据源
|
||||
databaseRegistrationBean.closeDatasource(dbDatasource.getId());
|
||||
// 验证新的数据源
|
||||
DbDatasource dbDatasourceSel = dbDatasourceService.getById(dbDatasource.getId());
|
||||
List<DatabaseFactoryBean> newFactoryBeanList = new LinkedList<>();
|
||||
List<DatabaseFactoryBean> databaseFactoryBeanList = databaseRegistrationBean.getDatabaseFactoryBeanList();
|
||||
for (DatabaseFactoryBean factoryBean : databaseFactoryBeanList) {
|
||||
if (Objects.equals(factoryBean.getId(), sourceId)) {
|
||||
try {
|
||||
// 关闭旧的数据源
|
||||
factoryBean.getDataSource().close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
newFactoryBeanList.add(factoryBean);
|
||||
}
|
||||
}
|
||||
if (Optional.ofNullable(dbDatasourceSel.getYn()).orElse(0) == 1) {
|
||||
if (Objects.equals(dbDatasourceSel.getYn(), 1)) {
|
||||
// 创建新的数据源
|
||||
DatabaseFactoryBean databaseFactoryBean = DatasourceUtil.createDatabaseFactoryBean(dbDatasourceSel);
|
||||
if (databaseFactoryBean != null) {
|
||||
newFactoryBeanList.add(databaseFactoryBean);
|
||||
}
|
||||
databaseRegistrationBean.setDatabaseFactoryBeanList(newFactoryBeanList);
|
||||
if (databaseFactoryBean == null) {
|
||||
return DocDbResponseJson.warn("创建数据源失败,请检查配置是否正确");
|
||||
}
|
||||
databaseFactoryBean.getDataSource().close();
|
||||
}
|
||||
return DocDbResponseJson.ok();
|
||||
}
|
||||
|
||||
@@ -1,40 +1,15 @@
|
||||
package com.zyplayer.doc.db.framework.configuration;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.DbDatasource;
|
||||
import com.zyplayer.doc.data.service.manage.DbDatasourceService;
|
||||
import com.zyplayer.doc.db.framework.db.bean.DatabaseFactoryBean;
|
||||
import com.zyplayer.doc.db.framework.db.bean.DatabaseRegistrationBean;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ApplicationListenerBean implements ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
@Resource
|
||||
DatabaseRegistrationBean databaseRegistrationBean;
|
||||
@Resource
|
||||
DbDatasourceService dbDatasourceService;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
List<DatabaseFactoryBean> databaseFactoryBeanList = new LinkedList<>();
|
||||
|
||||
QueryWrapper<DbDatasource> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("yn", 1);
|
||||
List<DbDatasource> datasourceList = dbDatasourceService.list(wrapper);
|
||||
for (DbDatasource dbDatasource : datasourceList) {
|
||||
DatabaseFactoryBean databaseFactoryBean = DatasourceUtil.createDatabaseFactoryBean(dbDatasource);
|
||||
if (databaseFactoryBean != null) {
|
||||
databaseFactoryBeanList.add(databaseFactoryBean);
|
||||
}
|
||||
}
|
||||
databaseRegistrationBean.setDatabaseFactoryBeanList(databaseFactoryBeanList);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,13 +16,11 @@ public class DatasourceUtil {
|
||||
|
||||
public static DatabaseFactoryBean createDatabaseFactoryBean(DbDatasource dbDatasource){
|
||||
try {
|
||||
// 数据源配置
|
||||
DruidDataSource dataSource = DruidDataSourceUtil.createDataSource(dbDatasource.getDriverClassName(), dbDatasource.getSourceUrl(), dbDatasource.getSourceName(), dbDatasource.getSourcePassword());
|
||||
Resource[] resources = null;
|
||||
// 描述连接信息的对象
|
||||
DatabaseFactoryBean databaseFactoryBean = new DatabaseFactoryBean();
|
||||
Resource[] resources = null;
|
||||
String dbUrl = dbDatasource.getSourceUrl();
|
||||
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
String dbUrl = dbDatasource.getSourceUrl();
|
||||
if (dbUrl.contains("mysql")) {
|
||||
// jdbc:mysql://192.168.0.1:3306/user_info?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
|
||||
String[] urlParamArr = dbUrl.split("\\?");
|
||||
@@ -62,6 +60,8 @@ public class DatasourceUtil {
|
||||
if (resources == null) {
|
||||
return null;
|
||||
}
|
||||
// 数据源配置
|
||||
DruidDataSource dataSource = DruidDataSourceUtil.createDataSource(dbDatasource.getDriverClassName(), dbDatasource.getSourceUrl(), dbDatasource.getSourceName(), dbDatasource.getSourcePassword());
|
||||
// 创建sqlSessionTemplate
|
||||
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
|
||||
sqlSessionFactoryBean.setDataSource(dataSource);
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
package com.zyplayer.doc.db.framework.db.bean;
|
||||
|
||||
import com.zyplayer.doc.core.exception.ConfirmException;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.DbDatasource;
|
||||
import com.zyplayer.doc.data.service.manage.DbDatasourceService;
|
||||
import com.zyplayer.doc.db.framework.configuration.DatasourceUtil;
|
||||
import com.zyplayer.doc.db.framework.db.mapper.base.BaseMapper;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 需要声明注入的对象,只需要设置dbConfigList即可
|
||||
* databaseFactoryBeanList是后面生成的
|
||||
* 所有的数据源管理类
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月8日
|
||||
*/
|
||||
@Repository
|
||||
public class DatabaseRegistrationBean {
|
||||
|
||||
// 注入此对象必须配置的参数
|
||||
// 配置的数据源连接、账号密码等信息,通过注入得到
|
||||
private List<DbConfigBean> dbConfigList = new LinkedList<>();
|
||||
@Resource
|
||||
DbDatasourceService dbDatasourceService;
|
||||
|
||||
// 描述连接信息的对象列表
|
||||
private List<DatabaseFactoryBean> databaseFactoryBeanList = new LinkedList<>();
|
||||
|
||||
public List<DatabaseFactoryBean> getDatabaseFactoryBeanList() {
|
||||
return databaseFactoryBeanList;
|
||||
}
|
||||
|
||||
public void setDatabaseFactoryBeanList(List<DatabaseFactoryBean> databaseFactoryBeanList) {
|
||||
this.databaseFactoryBeanList = databaseFactoryBeanList;
|
||||
}
|
||||
|
||||
public BaseMapper getBaseMapper(Long sourceId) {
|
||||
return getBaseMapper(sourceId, BaseMapper.class);
|
||||
}
|
||||
private Map<Long, DatabaseFactoryBean> databaseFactoryBeanMap = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取BaseMapper
|
||||
*
|
||||
* @param sourceId 数据源ID
|
||||
* @param cls 指定类
|
||||
* @return BaseMapper
|
||||
*/
|
||||
public <T> T getBaseMapper(Long sourceId, Class<T> cls) {
|
||||
DatabaseFactoryBean factoryBean = getFactoryById(sourceId);
|
||||
DatabaseFactoryBean factoryBean = getOrCreateFactoryById(sourceId);
|
||||
if (factoryBean != null) {
|
||||
SqlSessionTemplate sessionTemplate = factoryBean.getSqlSessionTemplate();
|
||||
try {
|
||||
@@ -47,17 +47,14 @@ public class DatabaseRegistrationBean {
|
||||
return null;
|
||||
}
|
||||
|
||||
public DatabaseFactoryBean getFactoryById(Long sourceId) {
|
||||
for (DatabaseFactoryBean databaseFactoryBean : databaseFactoryBeanList) {
|
||||
if (Objects.equals(databaseFactoryBean.getId(), sourceId)) {
|
||||
return databaseFactoryBean;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BaseMapper
|
||||
*
|
||||
* @param sourceId 数据源ID
|
||||
* @return BaseMapper
|
||||
*/
|
||||
public BaseMapper getBaseMapperById(Long sourceId) {
|
||||
DatabaseFactoryBean databaseFactoryBean = this.getFactoryById(sourceId);
|
||||
DatabaseFactoryBean databaseFactoryBean = this.getOrCreateFactoryById(sourceId);
|
||||
if (databaseFactoryBean == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -70,11 +67,53 @@ public class DatabaseRegistrationBean {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<DbConfigBean> getDbConfigList() {
|
||||
return dbConfigList;
|
||||
/**
|
||||
* 关闭数据源
|
||||
*
|
||||
* @param sourceId 数据源ID
|
||||
*/
|
||||
public void closeDatasource(Long sourceId) {
|
||||
DatabaseFactoryBean factoryBean = databaseFactoryBeanMap.remove(sourceId);
|
||||
if (factoryBean != null) {
|
||||
try {
|
||||
// 关闭数据源
|
||||
factoryBean.getDataSource().close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setDbConfigList(List<DbConfigBean> dbConfigList) {
|
||||
this.dbConfigList = dbConfigList;
|
||||
/**
|
||||
* 通过数据源ID获取或创建新的数据源
|
||||
*
|
||||
* @param sourceId 数据源ID
|
||||
* @return 数据源对象
|
||||
*/
|
||||
public DatabaseFactoryBean getOrCreateFactoryById(Long sourceId) {
|
||||
DatabaseFactoryBean factoryBean = databaseFactoryBeanMap.get(sourceId);
|
||||
if (factoryBean != null) return factoryBean;
|
||||
return this.createFactoryById(sourceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数据源的同步方法
|
||||
*
|
||||
* @param sourceId 数据源ID
|
||||
* @return 新数据源对象
|
||||
*/
|
||||
private synchronized DatabaseFactoryBean createFactoryById(Long sourceId) {
|
||||
DatabaseFactoryBean factoryBean = databaseFactoryBeanMap.get(sourceId);
|
||||
if (factoryBean != null) return factoryBean;
|
||||
DbDatasource dbDatasource = dbDatasourceService.getById(sourceId);
|
||||
if (dbDatasource == null) {
|
||||
throw new ConfirmException("未找到指定数据源配置信息:" + sourceId);
|
||||
}
|
||||
DatabaseFactoryBean databaseFactoryBean = DatasourceUtil.createDatabaseFactoryBean(dbDatasource);
|
||||
if (databaseFactoryBean == null) {
|
||||
throw new ConfirmException("创建数据源失败:" + sourceId);
|
||||
}
|
||||
databaseFactoryBeanMap.put(sourceId, databaseFactoryBean);
|
||||
return databaseFactoryBean;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class SqlExecutor {
|
||||
* @since 2019年8月18日
|
||||
*/
|
||||
public ExecuteResult execute(ExecuteParam param) {
|
||||
DatabaseFactoryBean factoryBean = databaseRegistrationBean.getFactoryById(param.getDatasourceId());
|
||||
DatabaseFactoryBean factoryBean = databaseRegistrationBean.getOrCreateFactoryById(param.getDatasourceId());
|
||||
return this.execute(factoryBean, param, null);
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ public class TransferDataServer {
|
||||
String querySql = transferTask.getQuerySql();
|
||||
String storageSql = transferTask.getStorageSql();
|
||||
List<Map<String, Object>> selectResultList = new LinkedList<>();
|
||||
DatabaseFactoryBean factoryBean = databaseRegistrationBean.getFactoryById(querySourceId);
|
||||
DatabaseFactoryBean factoryBean = databaseRegistrationBean.getOrCreateFactoryById(querySourceId);
|
||||
ExecuteParam executeParam = new ExecuteParam();
|
||||
executeParam.setDatasourceId(querySourceId);
|
||||
executeParam.setExecuteType(ExecuteType.SELECT);
|
||||
|
||||
Reference in New Issue
Block a user