Skip to content

Commit

Permalink
Default URL conflict with multiple data sources #28
Browse files Browse the repository at this point in the history
  • Loading branch information
Gelin Luo committed Feb 20, 2021
1 parent b9495c1 commit 8a5b23f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# act-sql-common CHANGE LOG

1.6.1
* Default URL conflict with multiple data sources #28

1.6.0 - 03/Nov/2019
* update to act-1.8.29

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/act/db/sql/DataSourceConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static act.Act.LOGGER;
import static act.db.sql.SqlConfKeys.*;

import act.app.DbServiceManager;
import act.data.annotation.Data;
import act.util.SimpleBean;
import org.osgl.$;
Expand Down Expand Up @@ -78,13 +79,13 @@ public class DataSourceConfig implements SimpleBean {
// Constructor for slave datasource
private DataSourceConfig(DataSourceConfig parent, Map<String, String> conf) {
$.copy(parent).to(this);
init(conf, true);
init(parent.id, conf, true);
readOnly = true;
}

public DataSourceConfig(String dbId, Map<String, String> conf) {
id = dbId;
init(conf, false);
init(id, conf, false);

// process for readonly data source
Map<String, String> soleSlaveConfig = new HashMap<>();
Expand Down Expand Up @@ -128,7 +129,7 @@ public boolean hasSoleSlave() {
return 1 == slaveDataSourceConfigurations.size();
}

protected void ensureEssentialDatasourceConfig(Map<String, String> conf) {
protected void ensureEssentialDatasourceConfig(String dbId, Map<String, String> conf) {
if (null == username) {
LOGGER.warn("No data source user configuration specified. Will use the default 'sa' user");
username = "sa";
Expand All @@ -143,7 +144,8 @@ protected void ensureEssentialDatasourceConfig(Map<String, String> conf) {
try {
Class.forName("org.h2.Driver");
LOGGER.warn("No database URL configuration specified. Will use the default h2 inmemory test database");
url = "jdbc:h2:./test";
String dbName = "test" + (DbServiceManager.DEFAULT.equals(dbId) ? "" : "-" + dbId);
url = "jdbc:h2:./" + dbName;
conf.put("url", url);
} catch (ClassNotFoundException e) {
throw new ConfigurationException("No database URL configuration specified to db service: " + id);
Expand Down Expand Up @@ -179,7 +181,7 @@ protected void ensureEssentialDatasourceConfig(Map<String, String> conf) {
}
}

private void init(Map<String, String> conf, boolean readOnly) {
private void init(String dbId, Map<String, String> conf, boolean readOnly) {
username = get(conf, SQL_CONF_USERNAME, username);
password = get(conf, SQL_CONF_PASSWORD, password);
driver = get(conf, SQL_CONF_DRIVER);
Expand All @@ -206,7 +208,7 @@ private void init(Map<String, String> conf, boolean readOnly) {
this.isolationLevel = isolationLevels.get(isoLevel);
}
}
ensureEssentialDatasourceConfig(conf);
ensureEssentialDatasourceConfig(dbId, conf);
customProperties = conf;
}

Expand Down

0 comments on commit 8a5b23f

Please sign in to comment.