Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improvement](jdbc catalog) Optimize connection pool caching logic #28859

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -281,6 +281,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 @@ -290,10 +292,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 @@ -312,11 +314,9 @@ private void init(String driverUrl, String sql, int batchSize, String driverClas
ds.setTimeBetweenEvictionRunsMillis(maxIdleTime / 5);
ds.setMinEvictableIdleTimeMillis(maxIdleTime);
druidDataSource = ds;
// here is a cache of datasource, which using the string(jdbcUrl + jdbcUser +
// 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
Loading