[New] PostgreSqlPersistenceHandler now expects a config for each realm
Now each realm which is CACHED or TRANSACTIONAL and thus uses a PersistenceHandler must have a dbUrl, dbUsername and dbPassword configured
This commit is contained in:
parent
7322894078
commit
be033f3e35
|
@ -48,6 +48,7 @@ public class DbConnectionCheck {
|
||||||
public void checkConnections() {
|
public void checkConnections() {
|
||||||
Collection<DbConnectionInfo> values = this.connetionInfoMap.values();
|
Collection<DbConnectionInfo> values = this.connetionInfoMap.values();
|
||||||
for (DbConnectionInfo connectionInfo : values) {
|
for (DbConnectionInfo connectionInfo : values) {
|
||||||
|
|
||||||
String url = connectionInfo.getUrl();
|
String url = connectionInfo.getUrl();
|
||||||
String username = connectionInfo.getUsername();
|
String username = connectionInfo.getUsername();
|
||||||
String password = connectionInfo.getPassword();
|
String password = connectionInfo.getPassword();
|
||||||
|
|
|
@ -15,6 +15,8 @@
|
||||||
*/
|
*/
|
||||||
package li.strolch.persistence.postgresql;
|
package li.strolch.persistence.postgresql;
|
||||||
|
|
||||||
|
import static ch.eitchnet.utils.helper.StringHelper.DOT;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.Driver;
|
import java.sql.Driver;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
|
@ -22,6 +24,7 @@ import java.sql.SQLException;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.agent.api.StrolchComponent;
|
import li.strolch.agent.api.StrolchComponent;
|
||||||
|
@ -59,33 +62,51 @@ public class PostgreSqlPersistenceHandler extends StrolchComponent implements Pe
|
||||||
this.componentConfiguration = componentConfiguration;
|
this.componentConfiguration = componentConfiguration;
|
||||||
this.connetionInfoMap = new HashMap<>();
|
this.connetionInfoMap = new HashMap<>();
|
||||||
|
|
||||||
String dbUrl = componentConfiguration.getString(PROP_DB_URL, null);
|
Set<String> realmNames = getContainer().getRealmNames();
|
||||||
String username = componentConfiguration.getString(PROP_DB_USERNAME, null);
|
for (String realmName : realmNames) {
|
||||||
String password = componentConfiguration.getString(PROP_DB_PASSWORD, null);
|
|
||||||
|
|
||||||
|
String dbUrlKey = PROP_DB_URL;
|
||||||
|
String dbUsernameKey = PROP_DB_USERNAME;
|
||||||
|
String dbPasswordKey = PROP_DB_PASSWORD;
|
||||||
|
if (!realmName.equals(StrolchConstants.DEFAULT_REALM)) {
|
||||||
|
dbUrlKey += DOT + realmName;
|
||||||
|
dbUsernameKey += DOT + realmName;
|
||||||
|
dbPasswordKey += DOT + realmName;
|
||||||
|
}
|
||||||
|
|
||||||
|
String dbUrl = componentConfiguration.getString(dbUrlKey, null);
|
||||||
|
String username = componentConfiguration.getString(dbUsernameKey, null);
|
||||||
|
String password = componentConfiguration.getString(dbPasswordKey, null);
|
||||||
|
|
||||||
|
DbConnectionInfo connectionInfo = new DbConnectionInfo(realmName, dbUrl);
|
||||||
|
connectionInfo.setUsername(username);
|
||||||
|
connectionInfo.setPassword(password);
|
||||||
|
|
||||||
|
loadDriverForConnection(connectionInfo);
|
||||||
|
this.connetionInfoMap.put(realmName, connectionInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
super.initialize(componentConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadDriverForConnection(DbConnectionInfo connectionInfo) {
|
||||||
Driver driver;
|
Driver driver;
|
||||||
try {
|
try {
|
||||||
// server loader does not seem to work in all contexts, thus:
|
// server loader does not seem to work in all contexts, thus:
|
||||||
org.postgresql.Driver.getLogLevel();
|
org.postgresql.Driver.getLogLevel();
|
||||||
|
|
||||||
driver = DriverManager.getDriver(dbUrl);
|
driver = DriverManager.getDriver(connectionInfo.getUrl());
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
String msg = "Failed to load DB driver for URL {0} due to: {1}"; //$NON-NLS-1$
|
String msg = "Failed to load DB driver for URL {0} due to: {1}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, dbUrl, e.getMessage());
|
msg = MessageFormat.format(msg, connectionInfo.getUrl(), e.getMessage());
|
||||||
throw new StrolchConfigurationException(msg, e);
|
throw new StrolchConfigurationException(msg, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
DbConnectionInfo connectionInfo = new DbConnectionInfo(StrolchConstants.DEFAULT_REALM, dbUrl);
|
|
||||||
connectionInfo.setUsername(username);
|
|
||||||
connectionInfo.setPassword(password);
|
|
||||||
this.connetionInfoMap.put(StrolchConstants.DEFAULT_REALM, connectionInfo);
|
|
||||||
|
|
||||||
String compliant = driver.jdbcCompliant() ? "" : "non"; //$NON-NLS-1$ //$NON-NLS-2$
|
String compliant = driver.jdbcCompliant() ? "" : "non"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
String msg = "Using {0} JDBC compliant Driver {1}.{2}"; //$NON-NLS-1$
|
String msg = "Using {0} JDBC compliant Driver {1}.{2}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, compliant, driver.getMajorVersion(), driver.getMinorVersion());
|
msg = MessageFormat.format(msg, compliant, driver.getMajorVersion(), driver.getMinorVersion());
|
||||||
logger.info(msg);
|
logger.info(msg);
|
||||||
|
|
||||||
super.initialize(componentConfiguration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -96,7 +96,7 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
||||||
public void autoCloseableCommit() {
|
public void autoCloseableCommit() {
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.info("Committing TX..."); //$NON-NLS-1$
|
logger.info("Committing TX for realm " + getRealmName() + "..."); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
|
@ -127,13 +127,16 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
||||||
long txDuration = end - this.startTime;
|
long txDuration = end - this.startTime;
|
||||||
long closeDuration = end - start;
|
long closeDuration = end - start;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("TX has failed after "); //$NON-NLS-1$
|
sb.append("TX for realm ");
|
||||||
|
sb.append(getRealmName());
|
||||||
|
sb.append(" has failed after "); //$NON-NLS-1$
|
||||||
sb.append(StringHelper.formatNanoDuration(txDuration));
|
sb.append(StringHelper.formatNanoDuration(txDuration));
|
||||||
sb.append(" with close operation taking "); //$NON-NLS-1$
|
sb.append(" with close operation taking "); //$NON-NLS-1$
|
||||||
sb.append(StringHelper.formatNanoDuration(closeDuration));
|
sb.append(StringHelper.formatNanoDuration(closeDuration));
|
||||||
logger.info(sb.toString());
|
logger.info(sb.toString());
|
||||||
|
|
||||||
throw new StrolchPersistenceException("Strolch Transaction failed due to " + e.getMessage(), e); //$NON-NLS-1$
|
throw new StrolchPersistenceException(
|
||||||
|
"Strolch Transaction for realm " + getRealmName() + " failed due to " + e.getMessage(), e); //$NON-NLS-1$
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
|
@ -153,7 +156,9 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
||||||
this.txResult.setRealm(getRealm().getRealm());
|
this.txResult.setRealm(getRealm().getRealm());
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("TX was completed after "); //$NON-NLS-1$
|
sb.append("TX for realm ");
|
||||||
|
sb.append(getRealmName());
|
||||||
|
sb.append(" was completed after "); //$NON-NLS-1$
|
||||||
sb.append(StringHelper.formatNanoDuration(txDuration));
|
sb.append(StringHelper.formatNanoDuration(txDuration));
|
||||||
sb.append(" with close operation taking "); //$NON-NLS-1$
|
sb.append(" with close operation taking "); //$NON-NLS-1$
|
||||||
sb.append(StringHelper.formatNanoDuration(closeDuration));
|
sb.append(StringHelper.formatNanoDuration(closeDuration));
|
||||||
|
|
|
@ -27,7 +27,7 @@ import li.strolch.testbase.runtime.RuntimeMock;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
public class XmlCachedDaoTest extends AbstractModelTest {
|
public class CachedDaoTest extends AbstractModelTest {
|
||||||
|
|
||||||
public static final String RUNTIME_PATH = "target/cachedStrolchRuntime/"; //$NON-NLS-1$
|
public static final String RUNTIME_PATH = "target/cachedStrolchRuntime/"; //$NON-NLS-1$
|
||||||
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
|
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
|
|
@ -60,7 +60,7 @@ public class ObserverUpdateTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() throws SQLException {
|
public static void beforeClass() throws SQLException {
|
||||||
|
|
||||||
XmlCachedDaoTest.dropSchema();
|
CachedDaoTest.dropSchema();
|
||||||
|
|
||||||
File rootPath = new File(RUNTIME_PATH);
|
File rootPath = new File(RUNTIME_PATH);
|
||||||
File configSrc = new File(CONFIG_SRC);
|
File configSrc = new File(CONFIG_SRC);
|
||||||
|
|
|
@ -24,7 +24,7 @@ import li.strolch.testbase.runtime.RuntimeMock;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
public class XmlTransactionalDaoTest extends AbstractModelTest {
|
public class TransactionalDaoTest extends AbstractModelTest {
|
||||||
|
|
||||||
public static final String RUNTIME_PATH = "target/transactionalStrolchRuntime/"; //$NON-NLS-1$
|
public static final String RUNTIME_PATH = "target/transactionalStrolchRuntime/"; //$NON-NLS-1$
|
||||||
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
|
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
|
||||||
|
@ -40,7 +40,7 @@ public class XmlTransactionalDaoTest extends AbstractModelTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() throws SQLException {
|
public static void beforeClass() throws SQLException {
|
||||||
|
|
||||||
XmlCachedDaoTest.dropSchema();
|
CachedDaoTest.dropSchema();
|
||||||
|
|
||||||
File rootPath = new File(RUNTIME_PATH);
|
File rootPath = new File(RUNTIME_PATH);
|
||||||
File configSrc = new File(CONFIG_SRC);
|
File configSrc = new File(CONFIG_SRC);
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<StrolchConfiguration>
|
||||||
|
<Runtime>
|
||||||
|
<applicationName>StrolchPersistenceTest</applicationName>
|
||||||
|
<Properties>
|
||||||
|
<verbose>true</verbose>
|
||||||
|
</Properties>
|
||||||
|
</Runtime>
|
||||||
|
<Component>
|
||||||
|
<name>RealmHandler</name>
|
||||||
|
<api>li.strolch.agent.api.RealmHandler</api>
|
||||||
|
<impl>li.strolch.agent.impl.DefaultRealmHandler</impl>
|
||||||
|
<depends>PersistenceHandler</depends>
|
||||||
|
<Properties>
|
||||||
|
<realms>first, second</realms>
|
||||||
|
<dataStoreMode.first>TRANSACTIONAL</dataStoreMode.first>
|
||||||
|
<dataStoreMode.second>TRANSACTIONAL</dataStoreMode.second>
|
||||||
|
</Properties>
|
||||||
|
</Component>
|
||||||
|
<Component>
|
||||||
|
<name>PersistenceHandler</name>
|
||||||
|
<api>li.strolch.persistence.api.PersistenceHandler</api>
|
||||||
|
<impl>li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler</impl>
|
||||||
|
<Properties>
|
||||||
|
<allowSchemaCreation>true</allowSchemaCreation>
|
||||||
|
<allowSchemaDrop>true</allowSchemaDrop>
|
||||||
|
|
||||||
|
<db.url.first>jdbc:postgresql://localhost/testdb</db.url.first>
|
||||||
|
<db.username.first>testuser</db.username.first>
|
||||||
|
<db.password.first>test</db.password.first>
|
||||||
|
|
||||||
|
<db.url.second>jdbc:postgresql://localhost/testdb</db.url.second>
|
||||||
|
<db.username.second>testuser</db.username.second>
|
||||||
|
<db.password.second>test</db.password.second>
|
||||||
|
</Properties>
|
||||||
|
</Component>
|
||||||
|
<Component>
|
||||||
|
<name>ObserverHandler</name>
|
||||||
|
<api>li.strolch.runtime.observer.ObserverHandler</api>
|
||||||
|
<impl>li.strolch.runtime.observer.DefaultObserverHandler</impl>
|
||||||
|
</Component>
|
||||||
|
</StrolchConfiguration>
|
Loading…
Reference in New Issue