update pool
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
package com.anjiplus.template.gaea.business.modules.dataSource.pool.exception;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class JdbcPoolException extends RuntimeException {
|
||||
|
||||
public JdbcPoolException() {
|
||||
}
|
||||
|
||||
public JdbcPoolException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JdbcPoolException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public JdbcPoolException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public JdbcPoolException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,9 +16,9 @@ import com.anjiplus.template.gaea.business.modules.dataSource.controller.dto.Dat
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.controller.param.ConnectionParam;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.dao.DataSourceMapper;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.dao.entity.DataSource;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.pool.constant.JdbcConstants;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.pool.util.JdbcUtil;
|
||||
import com.anjiplus.template.gaea.business.util.JdbcConstants;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.service.DataSourceService;
|
||||
import com.anjiplus.template.gaea.business.util.JdbcUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.anjiplus.template.gaea.business.modules.dataSource.pool.util;
|
||||
package com.anjiplus.template.gaea.business.util;
|
||||
|
||||
import com.anji.plus.gaea.exception.BusinessExceptionBuilder;
|
||||
import com.anjiplus.template.gaea.business.code.ResponseCode;
|
||||
@@ -1,9 +1,22 @@
|
||||
package com.anjiplus.template.gaea.business.modules.dataSource.pool.constant;
|
||||
package com.anjiplus.template.gaea.business.util;
|
||||
|
||||
/**
|
||||
* Created by raodeming on 2021/3/19.
|
||||
*/
|
||||
public class JdbcConstants {
|
||||
/**
|
||||
* 已支持的数据源
|
||||
*/
|
||||
public final static String KUDU_IMAPLA = "kudu_impala";
|
||||
public final static String HTTP = "http";
|
||||
public final static String MYSQL = "mysql";
|
||||
public final static String ORACLE = "oracle";
|
||||
public final static String ELASTIC_SEARCH_SQL = "elasticsearch_sql";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public final static String JTDS = "jtds";
|
||||
public final static String MOCK = "mock";
|
||||
public final static String HSQL = "hsql";
|
||||
@@ -16,12 +29,10 @@ public class JdbcConstants {
|
||||
public final static String SQL_SERVER_DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
|
||||
public final static String SQL_SERVER_DRIVER_SQLJDBC4 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
||||
public final static String SQL_SERVER_DRIVER_JTDS = "net.sourceforge.jtds.jdbc.Driver";
|
||||
public final static String ORACLE = "oracle";
|
||||
public final static String ORACLE_DRIVER = "oracle.jdbc.OracleDriver";
|
||||
public final static String ORACLE_DRIVER2 = "oracle.jdbc.driver.OracleDriver";
|
||||
public final static String ALI_ORACLE = "AliOracle";
|
||||
public final static String ALI_ORACLE_DRIVER = "com.alibaba.jdbc.AlibabaDriver";
|
||||
public final static String MYSQL = "mysql";
|
||||
public final static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
|
||||
public final static String MYSQL_DRIVER_6 = "com.mysql.cj.jdbc.Driver";
|
||||
public final static String MYSQL_DRIVER_REPLICATE = "com.mysql.jdbc.";
|
||||
@@ -62,10 +73,8 @@ public class JdbcConstants {
|
||||
public final static String ALIYUN_DRDS = "aliyun_drds";
|
||||
public final static String PRESTO = "presto";
|
||||
public final static String ELASTIC_SEARCH = "elasticsearch";
|
||||
public final static String ELASTIC_SEARCH_SQL = "elasticsearch_sql";
|
||||
public final static String ELASTIC_SEARCH_DRIVER = "com.alibaba.xdriver.elastic.jdbc.ElasticDriver";
|
||||
public final static String CLICKHOUSE = "clickhouse";
|
||||
public final static String CLICKHOUSE_DRIVER = "ru.yandex.clickhouse.ClickHouseDriver";
|
||||
public final static String KUDU_IMAPLA = "kudu_impala";
|
||||
public final static String HTTP = "http";
|
||||
|
||||
}
|
||||
@@ -1,18 +1,15 @@
|
||||
package com.anjiplus.template.gaea.business.modules.dataSource.pool.util;
|
||||
package com.anjiplus.template.gaea.business.util;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.controller.dto.DataSourceDto;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.pool.datasource.PooledDataSource;
|
||||
import com.anjiplus.template.gaea.business.modules.dataSource.pool.datasource.UnPooledDataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* Created by raodeming on 2021/3/18.
|
||||
@@ -20,7 +17,9 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
@Slf4j
|
||||
public class JdbcUtil {
|
||||
|
||||
//所有数据源的连接池存在map里
|
||||
/**
|
||||
* 所有数据源的连接池存在map里
|
||||
*/
|
||||
static Map<Long, DruidDataSource> map = new ConcurrentHashMap<>();
|
||||
|
||||
public static DruidDataSource getJdbcConnectionPool(DataSourceDto dataSource) {
|
||||
@@ -48,13 +47,14 @@ public class JdbcUtil {
|
||||
log.info("创建连接池成功:{}", dataSource.getJdbcUrl());
|
||||
}
|
||||
return map.get(dataSource.getId());
|
||||
} finally {
|
||||
} finally {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据库连接池
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
public static void removeJdbcConnectionPool(Long id) {
|
||||
@@ -65,13 +65,14 @@ public class JdbcUtil {
|
||||
map.remove(id);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("error",e);
|
||||
log.error("error", e);
|
||||
} finally {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
* @throws SQLException
|
||||
@@ -83,20 +84,20 @@ public class JdbcUtil {
|
||||
|
||||
/**
|
||||
* 测试数据库连接 获取一个连接
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
* @throws ClassNotFoundException driverName不正确
|
||||
* @throws ClassNotFoundException driverName不正确
|
||||
* @throws SQLException
|
||||
*/
|
||||
public static Connection getUnPooledConnection(DataSourceDto dataSource) throws SQLException {
|
||||
UnPooledDataSource source = new UnPooledDataSource();
|
||||
source.setJdbcUrl(dataSource.getJdbcUrl());
|
||||
source.setDriverClass(dataSource.getDriverName());
|
||||
source.setUser(dataSource.getUsername());
|
||||
source.setPassword(dataSource.getPassword());
|
||||
return source.getConnection();
|
||||
DriverClassUtil.loadDriverClass(dataSource.getDriverName(), dataSource.getJdbcUrl());
|
||||
if (StringUtils.isBlank(dataSource.getUsername()) && StringUtils.isBlank(dataSource.getPassword())) {
|
||||
return DriverManager.getConnection(dataSource.getJdbcUrl());
|
||||
}
|
||||
return DriverManager.getConnection(dataSource.getJdbcUrl(),
|
||||
dataSource.getUsername(), dataSource.getPassword());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user