From 8a5b23f2b4d0043f78fa1195e8cc0c5e0e3e52aa Mon Sep 17 00:00:00 2001 From: Gelin Luo Date: Sun, 21 Feb 2021 08:39:33 +1100 Subject: [PATCH] Default URL conflict with multiple data sources #28 --- CHANGELOG.md | 3 +++ src/main/java/act/db/sql/DataSourceConfig.java | 14 ++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 625752d..3c28899 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/act/db/sql/DataSourceConfig.java b/src/main/java/act/db/sql/DataSourceConfig.java index 8c2a755..179cca0 100644 --- a/src/main/java/act/db/sql/DataSourceConfig.java +++ b/src/main/java/act/db/sql/DataSourceConfig.java @@ -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.$; @@ -78,13 +79,13 @@ public class DataSourceConfig implements SimpleBean { // Constructor for slave datasource private DataSourceConfig(DataSourceConfig parent, Map conf) { $.copy(parent).to(this); - init(conf, true); + init(parent.id, conf, true); readOnly = true; } public DataSourceConfig(String dbId, Map conf) { id = dbId; - init(conf, false); + init(id, conf, false); // process for readonly data source Map soleSlaveConfig = new HashMap<>(); @@ -128,7 +129,7 @@ public boolean hasSoleSlave() { return 1 == slaveDataSourceConfigurations.size(); } - protected void ensureEssentialDatasourceConfig(Map conf) { + protected void ensureEssentialDatasourceConfig(String dbId, Map conf) { if (null == username) { LOGGER.warn("No data source user configuration specified. Will use the default 'sa' user"); username = "sa"; @@ -143,7 +144,8 @@ protected void ensureEssentialDatasourceConfig(Map 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); @@ -179,7 +181,7 @@ protected void ensureEssentialDatasourceConfig(Map conf) { } } - private void init(Map conf, boolean readOnly) { + private void init(String dbId, Map conf, boolean readOnly) { username = get(conf, SQL_CONF_USERNAME, username); password = get(conf, SQL_CONF_PASSWORD, password); driver = get(conf, SQL_CONF_DRIVER); @@ -206,7 +208,7 @@ private void init(Map conf, boolean readOnly) { this.isolationLevel = isolationLevels.get(isoLevel); } } - ensureEssentialDatasourceConfig(conf); + ensureEssentialDatasourceConfig(dbId, conf); customProperties = conf; }