diff --git a/backend/src/main/java/io/dataease/datasource/provider/DatasourceProvider.java b/backend/src/main/java/io/dataease/datasource/provider/DatasourceProvider.java index 275f6a60ff..62cf6476cc 100644 --- a/backend/src/main/java/io/dataease/datasource/provider/DatasourceProvider.java +++ b/backend/src/main/java/io/dataease/datasource/provider/DatasourceProvider.java @@ -19,7 +19,7 @@ public abstract class DatasourceProvider { return new ArrayList<>(); }; - public void test(DatasourceRequest datasourceRequest) throws Exception { + public void checkStatus(DatasourceRequest datasourceRequest) throws Exception { getData(datasourceRequest); } @@ -29,5 +29,5 @@ public abstract class DatasourceProvider { abstract public Map fetchResultAndField(DatasourceRequest datasourceRequest) throws Exception; - abstract public void initDataSource(DatasourceRequest datasourceRequest, String type) throws Exception; + abstract public void handleDatasource(DatasourceRequest datasourceRequest, String type) throws Exception; } diff --git a/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java b/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java index fa8ab84c91..98f70cb58d 100644 --- a/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java +++ b/backend/src/main/java/io/dataease/datasource/provider/JdbcProvider.java @@ -19,8 +19,8 @@ import java.util.*; public class JdbcProvider extends DatasourceProvider { private static Map jdbcConnection = new HashMap<>(); - private static int initPoolSize = 1; - private static int maxConnections = 1; + private static int initPoolSize = 5; + private static int maxConnections = 200; /** * 增加缓存机制 key 由 'provider_sql_' dsr.datasource.id dsr.table dsr.query共4部分组成,命中则使用缓存直接返回不再执行sql逻辑 @@ -268,7 +268,7 @@ public class JdbcProvider extends DatasourceProvider { } @Override - public void test(DatasourceRequest datasourceRequest) throws Exception { + public void checkStatus(DatasourceRequest datasourceRequest) throws Exception { String queryStr = getTablesSql(datasourceRequest); Connection con = null; try { @@ -305,7 +305,7 @@ public class JdbcProvider extends DatasourceProvider { private Connection getConnectionFromPool(DatasourceRequest datasourceRequest) throws Exception { ComboPooledDataSource dataSource = jdbcConnection.get(datasourceRequest.getDatasource().getId()); if (dataSource == null) { - initDataSource(datasourceRequest, "add"); + handleDatasource(datasourceRequest, "add"); } dataSource = jdbcConnection.get(datasourceRequest.getDatasource().getId()); Connection co = dataSource.getConnection(); @@ -313,27 +313,36 @@ public class JdbcProvider extends DatasourceProvider { } @Override - public void initDataSource(DatasourceRequest datasourceRequest, String type) throws Exception { + public void handleDatasource(DatasourceRequest datasourceRequest, String type) throws Exception { + ComboPooledDataSource dataSource = null; switch (type){ case "add": - ComboPooledDataSource dataSource = jdbcConnection.get(datasourceRequest.getDatasource().getId()); + checkStatus(datasourceRequest); + dataSource = jdbcConnection.get(datasourceRequest.getDatasource().getId()); if (dataSource == null) { - extracted(datasourceRequest); + addToPool(datasourceRequest); } break; case "edit": - jdbcConnection.remove(datasourceRequest.getDatasource().getId()); - extracted(datasourceRequest); + dataSource = jdbcConnection.get(datasourceRequest.getDatasource().getId()); + if (dataSource != null) { + dataSource.close(); + } + checkStatus(datasourceRequest); + addToPool(datasourceRequest); break; case "delete": - jdbcConnection.remove(datasourceRequest.getDatasource().getId()); + dataSource = jdbcConnection.get(datasourceRequest.getDatasource().getId()); + if (dataSource != null) { + dataSource.close(); + } break; default: break; } } - private void extracted(DatasourceRequest datasourceRequest) throws PropertyVetoException { + private void addToPool(DatasourceRequest datasourceRequest) throws PropertyVetoException { ComboPooledDataSource dataSource; dataSource = new ComboPooledDataSource(); setCredential(datasourceRequest, dataSource); diff --git a/backend/src/main/java/io/dataease/datasource/service/DatasourceService.java b/backend/src/main/java/io/dataease/datasource/service/DatasourceService.java index 921e7d2b61..ca40861717 100644 --- a/backend/src/main/java/io/dataease/datasource/service/DatasourceService.java +++ b/backend/src/main/java/io/dataease/datasource/service/DatasourceService.java @@ -56,20 +56,20 @@ public class DatasourceService { datasource.setCreateTime(currentTimeMillis); datasource.setCreateBy(String.valueOf(AuthUtils.getUser().getUsername())); datasourceMapper.insertSelective(datasource); - initConnectionPool(datasource, "add"); + handleConnectionPool(datasource, "add"); return datasource; } - private void initConnectionPool(Datasource datasource, String type) { + private void handleConnectionPool(Datasource datasource, String type) { commonThreadPool.addTask(() -> { try { DatasourceProvider datasourceProvider = ProviderFactory.getProvider(datasource.getType()); DatasourceRequest datasourceRequest = new DatasourceRequest(); datasourceRequest.setDatasource(datasource); - datasourceProvider.initDataSource(datasourceRequest, type); - LogUtil.info("Succsss to init datasource connection pool: " + datasource.getName()); + datasourceProvider.handleDatasource(datasourceRequest, type); + LogUtil.info("Succsss to {} datasource connection pool: {}", type, datasource.getName()); } catch (Exception e) { - LogUtil.error("Failed to init datasource connection pool: " + datasource.getName(), e); + LogUtil.error("Failed to handle datasource connection pool: " + datasource.getName(), e); } }); } @@ -100,7 +100,9 @@ public class DatasourceService { if(CollectionUtils.isNotEmpty(datasetTables)){ DataEaseException.throwException(datasetTables.size() + Translator.get("i18n_datasource_not_allow_delete_msg")); } + Datasource datasource = datasourceMapper.selectByPrimaryKey(datasourceId); datasourceMapper.deleteByPrimaryKey(datasourceId); + handleConnectionPool(datasource, "delete"); } public void updateDatasource(Datasource datasource) { @@ -108,14 +110,14 @@ public class DatasourceService { datasource.setCreateTime(null); datasource.setUpdateTime(System.currentTimeMillis()); datasourceMapper.updateByPrimaryKeySelective(datasource); - initConnectionPool(datasource, "edit"); + handleConnectionPool(datasource, "edit"); } public void validate(Datasource datasource) throws Exception { DatasourceProvider datasourceProvider = ProviderFactory.getProvider(datasource.getType()); DatasourceRequest datasourceRequest = new DatasourceRequest(); datasourceRequest.setDatasource(datasource); - datasourceProvider.test(datasourceRequest); + datasourceProvider.checkStatus(datasourceRequest); } public List getTables(Datasource datasource) throws Exception { @@ -165,7 +167,7 @@ public class DatasourceService { List datasources = datasourceMapper.selectByExampleWithBLOBs(new DatasourceExample()); datasources.forEach(datasource -> { try { - initConnectionPool(datasource, "add"); + handleConnectionPool(datasource, "add"); } catch (Exception e) { e.printStackTrace(); }