Skip to content

Commit

Permalink
For #12634, Fix Nacos additional data source does not support configu…
Browse files Browse the repository at this point in the history
…ration of database driver, resulting in loading error
  • Loading branch information
silent-night-no-trace committed Sep 14, 2024
1 parent 3a9003b commit 9224ee4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class ExternalDataSourceProperties {

private List<String> password = new ArrayList<>();

private List<String> driverClassName = new ArrayList<>();

public void setNum(Integer num) {
this.num = num;
}
Expand All @@ -65,6 +67,10 @@ public void setPassword(List<String> password) {
this.password = password;
}

public void setDriverClassName(List<String> driverClassName) {
this.driverClassName = driverClassName;
}

/**
* Build serveral HikariDataSource.
*
Expand All @@ -82,9 +88,8 @@ List<HikariDataSource> build(Environment environment, Callback<HikariDataSource>
int currentSize = index + 1;
Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index);
DataSourcePoolProperties poolProperties = DataSourcePoolProperties.build(environment);
if (StringUtils.isEmpty(poolProperties.getDataSource().getDriverClassName())) {
poolProperties.setDriverClassName(JDBC_DRIVER_NAME);
}
// 支持不同数据库配置不同的数据库驱动
poolProperties.setDriverClassName(getOrDefault(driverClassName, index, JDBC_DRIVER_NAME).trim());
poolProperties.setJdbcUrl(url.get(index).trim());
poolProperties.setUsername(getOrDefault(user, index, user.get(0)).trim());
poolProperties.setPassword(getOrDefault(password, index, password.get(0)).trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class ExternalDataSourcePropertiesTest {

public static final String USERNAME = "nacos_devtest";

private static final String JDBC_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";

private static final String KINGBASE_JDBC_DRIVER_NAME = "com.kingbase8.Driver";

@Test
void externalDatasourceNormally() {
HikariDataSource expectedDataSource = new HikariDataSource();
Expand Down Expand Up @@ -148,4 +152,27 @@ void externalDatasourceFailureWithErrorInfo() {
});
}

@Test
void externalDatasourceToAssertDriverClassName() {
try (HikariDataSource expectedDataSource = new HikariDataSource()) {
expectedDataSource.setJdbcUrl(JDBC_URL);
expectedDataSource.setUsername(USERNAME);
expectedDataSource.setPassword(PASSWORD);
expectedDataSource.setDriverClassName(JDBC_DRIVER_NAME);
MockEnvironment environment = new MockEnvironment();
// error num of db
environment.setProperty("db.num", "1");
environment.setProperty("db.url", JDBC_URL);
environment.setProperty("db.user", USERNAME);
environment.setProperty("db.password", PASSWORD);
environment.setProperty("db.driverClassName", JDBC_DRIVER_NAME);
List<HikariDataSource> dataSources = new ExternalDataSourceProperties().build(environment, (dataSource -> {
assertEquals(dataSource.getJdbcUrl(), expectedDataSource.getJdbcUrl());
assertEquals(dataSource.getUsername(), expectedDataSource.getUsername());
assertEquals(dataSource.getPassword(), expectedDataSource.getPassword());
assertEquals(dataSource.getDataSourceClassName(), expectedDataSource.getDataSourceClassName());

}));
}
}
}

0 comments on commit 9224ee4

Please sign in to comment.