forked from github/dataease
Merge pull request #135 from dataease/pr@dev@oracle
feat: 删除数据源时,同时释放掉连接池
This commit is contained in:
commit
b9ac54b67e
@ -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<String, List> 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;
|
||||
}
|
||||
|
@ -19,8 +19,8 @@ import java.util.*;
|
||||
public class JdbcProvider extends DatasourceProvider {
|
||||
|
||||
private static Map<String, ComboPooledDataSource> 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);
|
||||
|
@ -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<DBTableDTO> getTables(Datasource datasource) throws Exception {
|
||||
@ -165,7 +167,7 @@ public class DatasourceService {
|
||||
List<Datasource> datasources = datasourceMapper.selectByExampleWithBLOBs(new DatasourceExample());
|
||||
datasources.forEach(datasource -> {
|
||||
try {
|
||||
initConnectionPool(datasource, "add");
|
||||
handleConnectionPool(datasource, "add");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user