[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:
Robert von Burg 2014-02-19 00:11:14 +01:00
parent 7322894078
commit be033f3e35
7 changed files with 88 additions and 19 deletions

View File

@ -48,6 +48,7 @@ public class DbConnectionCheck {
public void checkConnections() {
Collection<DbConnectionInfo> values = this.connetionInfoMap.values();
for (DbConnectionInfo connectionInfo : values) {
String url = connectionInfo.getUrl();
String username = connectionInfo.getUsername();
String password = connectionInfo.getPassword();

View File

@ -15,6 +15,8 @@
*/
package li.strolch.persistence.postgresql;
import static ch.eitchnet.utils.helper.StringHelper.DOT;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
@ -22,6 +24,7 @@ import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchComponent;
@ -59,33 +62,51 @@ public class PostgreSqlPersistenceHandler extends StrolchComponent implements Pe
this.componentConfiguration = componentConfiguration;
this.connetionInfoMap = new HashMap<>();
String dbUrl = componentConfiguration.getString(PROP_DB_URL, null);
String username = componentConfiguration.getString(PROP_DB_USERNAME, null);
String password = componentConfiguration.getString(PROP_DB_PASSWORD, null);
Set<String> realmNames = getContainer().getRealmNames();
for (String realmName : realmNames) {
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;
try {
// server loader does not seem to work in all contexts, thus:
org.postgresql.Driver.getLogLevel();
driver = DriverManager.getDriver(dbUrl);
driver = DriverManager.getDriver(connectionInfo.getUrl());
} catch (SQLException e) {
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);
}
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 msg = "Using {0} JDBC compliant Driver {1}.{2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, compliant, driver.getMajorVersion(), driver.getMinorVersion());
logger.info(msg);
super.initialize(componentConfiguration);
}
@Override

View File

@ -96,7 +96,7 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
public void autoCloseableCommit() {
if (logger.isDebugEnabled()) {
logger.info("Committing TX..."); //$NON-NLS-1$
logger.info("Committing TX for realm " + getRealmName() + "..."); //$NON-NLS-1$
}
long start = System.nanoTime();
@ -127,13 +127,16 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
long txDuration = end - this.startTime;
long closeDuration = end - start;
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(" with close operation taking "); //$NON-NLS-1$
sb.append(StringHelper.formatNanoDuration(closeDuration));
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 {
try {
@ -153,7 +156,9 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
this.txResult.setRealm(getRealm().getRealm());
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(" with close operation taking "); //$NON-NLS-1$
sb.append(StringHelper.formatNanoDuration(closeDuration));

View File

@ -27,7 +27,7 @@ import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.AfterClass;
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 DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$

View File

@ -60,7 +60,7 @@ public class ObserverUpdateTest {
@BeforeClass
public static void beforeClass() throws SQLException {
XmlCachedDaoTest.dropSchema();
CachedDaoTest.dropSchema();
File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC);

View File

@ -24,7 +24,7 @@ import li.strolch.testbase.runtime.RuntimeMock;
import org.junit.AfterClass;
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 DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
@ -40,7 +40,7 @@ public class XmlTransactionalDaoTest extends AbstractModelTest {
@BeforeClass
public static void beforeClass() throws SQLException {
XmlCachedDaoTest.dropSchema();
CachedDaoTest.dropSchema();
File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC);

View File

@ -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>