Skip to content

Commit

Permalink
[improvement](jdbc catalog) Optimize connection pool caching logic #2…
Browse files Browse the repository at this point in the history
  • Loading branch information
zy-kkk authored Dec 26, 2023
1 parent 76b5470 commit b381e17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ public static JdbcDataSource getDataSource() {
return jdbcDataSource;
}

public DruidDataSource getSource(String jdbcUrl) {
return sourcesMap.get(jdbcUrl);
public DruidDataSource getSource(String cacheKey) {
return sourcesMap.get(cacheKey);
}

public void putSource(String jdbcUrl, DruidDataSource ds) {
sourcesMap.put(jdbcUrl, ds);
public void putSource(String cacheKey, DruidDataSource ds) {
sourcesMap.put(cacheKey, ds);
}

public Map<String, DruidDataSource> getSourcesMap() {
return sourcesMap;
}

public String createCacheKey(String jdbcUrl, String jdbcUser, String jdbcPassword, String jdbcDriverUrl,
String jdbcDriverClass) {
return jdbcUrl + jdbcUser + jdbcPassword + jdbcDriverUrl + jdbcDriverClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ public boolean hasNext() throws UdfRuntimeException {

private void init(String driverUrl, String sql, int batchSize, String driverClass, String jdbcUrl, String jdbcUser,
String jdbcPassword, TJdbcOperation op, TOdbcTableType tableType) throws UdfRuntimeException {
String druidDataSourceKey = JdbcDataSource.getDataSource().createCacheKey(jdbcUrl, jdbcUser, jdbcPassword,
driverUrl, driverClass);
try {
if (isNebula()) {
batchSizeNum = batchSize;
Expand All @@ -418,10 +420,10 @@ private void init(String driverUrl, String sql, int batchSize, String driverClas
} else {
ClassLoader parent = getClass().getClassLoader();
ClassLoader classLoader = UdfUtils.getClassLoader(driverUrl, parent);
druidDataSource = JdbcDataSource.getDataSource().getSource(jdbcUrl + jdbcUser + jdbcPassword);
druidDataSource = JdbcDataSource.getDataSource().getSource(druidDataSourceKey);
if (druidDataSource == null) {
synchronized (druidDataSourceLock) {
druidDataSource = JdbcDataSource.getDataSource().getSource(jdbcUrl + jdbcUser + jdbcPassword);
druidDataSource = JdbcDataSource.getDataSource().getSource(druidDataSourceKey);
if (druidDataSource == null) {
long start = System.currentTimeMillis();
DruidDataSource ds = new DruidDataSource();
Expand All @@ -444,7 +446,7 @@ private void init(String driverUrl, String sql, int batchSize, String driverClas
// jdbcPassword) as key.
// and the default datasource init = 1, min = 1, max = 100, if one of connection idle
// time greater than 10 minutes. then connection will be retrieved.
JdbcDataSource.getDataSource().putSource(jdbcUrl + jdbcUser + jdbcPassword, ds);
JdbcDataSource.getDataSource().putSource(druidDataSourceKey, ds);
LOG.info("init datasource [" + (jdbcUrl + jdbcUser) + "] cost: " + (
System.currentTimeMillis() - start) + " ms");
}
Expand Down

0 comments on commit b381e17

Please sign in to comment.