[Major] refactored how transactions are opened

Now the ElementMapHandler is the central object and clients do not use
access DAOs or the PersistenceHandler anymore. This is now transparent.
This commit is contained in:
Robert von Burg 2014-01-10 19:10:42 +01:00
parent 64fed84708
commit 8c91c12948
3 changed files with 12 additions and 15 deletions

View File

@ -30,6 +30,7 @@ import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.runtime.StrolchConstants; import li.strolch.runtime.StrolchConstants;
import li.strolch.runtime.agent.api.StrolchComponent; import li.strolch.runtime.agent.api.StrolchComponent;
import li.strolch.runtime.agent.impl.ComponentContainerImpl; import li.strolch.runtime.agent.impl.ComponentContainerImpl;
import li.strolch.runtime.agent.impl.StrolchRealm;
import li.strolch.runtime.configuration.ComponentConfiguration; import li.strolch.runtime.configuration.ComponentConfiguration;
import li.strolch.runtime.configuration.StrolchConfigurationException; import li.strolch.runtime.configuration.StrolchConfigurationException;
import li.strolch.runtime.observer.ObserverHandler; import li.strolch.runtime.observer.ObserverHandler;
@ -99,11 +100,7 @@ public class PostgreSqlPersistenceHandler extends StrolchComponent implements Pe
super.start(); super.start();
} }
public StrolchTransaction openTx() { public StrolchTransaction openTx(StrolchRealm realm) {
return openTx(StrolchConstants.DEFAULT_REALM);
}
public StrolchTransaction openTx(String realm) {
PostgreSqlStrolchTransaction tx = new PostgreSqlStrolchTransaction(realm, this); PostgreSqlStrolchTransaction tx = new PostgreSqlStrolchTransaction(realm, this);
if (getContainer().hasComponent(ObserverHandler.class)) { if (getContainer().hasComponent(ObserverHandler.class)) {
tx.setObserverHandler(getContainer().getComponent(ObserverHandler.class)); tx.setObserverHandler(getContainer().getComponent(ObserverHandler.class));

View File

@ -21,14 +21,15 @@ import java.util.Date;
import java.util.Set; import java.util.Set;
import li.strolch.model.StrolchElement; import li.strolch.model.StrolchElement;
import li.strolch.persistence.api.AbstractTransaction;
import li.strolch.persistence.api.ModificationResult; import li.strolch.persistence.api.ModificationResult;
import li.strolch.persistence.api.OrderDao; import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.ResourceDao; import li.strolch.persistence.api.ResourceDao;
import li.strolch.persistence.api.StrolchPersistenceException; import li.strolch.persistence.api.StrolchPersistenceException;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.persistence.api.TransactionCloseStrategy; import li.strolch.persistence.api.TransactionCloseStrategy;
import li.strolch.persistence.api.TransactionResult; import li.strolch.persistence.api.TransactionResult;
import li.strolch.persistence.api.TransactionState; import li.strolch.persistence.api.TransactionState;
import li.strolch.runtime.agent.impl.StrolchRealm;
import li.strolch.runtime.observer.ObserverHandler; import li.strolch.runtime.observer.ObserverHandler;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -36,11 +37,10 @@ import org.slf4j.LoggerFactory;
import ch.eitchnet.utils.helper.StringHelper; import ch.eitchnet.utils.helper.StringHelper;
public class PostgreSqlStrolchTransaction implements StrolchTransaction { public class PostgreSqlStrolchTransaction extends AbstractTransaction {
private static final Logger logger = LoggerFactory.getLogger(PostgreSqlStrolchTransaction.class); private static final Logger logger = LoggerFactory.getLogger(PostgreSqlStrolchTransaction.class);
private PostgreSqlPersistenceHandler persistenceHandler; private PostgreSqlPersistenceHandler persistenceHandler;
private String realm;
private TransactionCloseStrategy closeStrategy; private TransactionCloseStrategy closeStrategy;
private ObserverHandler observerHandler; private ObserverHandler observerHandler;
@ -54,10 +54,10 @@ public class PostgreSqlStrolchTransaction implements StrolchTransaction {
private TransactionResult txResult; private TransactionResult txResult;
private boolean open; private boolean open;
public PostgreSqlStrolchTransaction(String realm, PostgreSqlPersistenceHandler persistenceHandler) { public PostgreSqlStrolchTransaction(StrolchRealm realm, PostgreSqlPersistenceHandler persistenceHandler) {
super(realm);
this.startTime = System.nanoTime(); this.startTime = System.nanoTime();
this.startTimeDate = new Date(); this.startTimeDate = new Date();
this.realm = realm;
this.persistenceHandler = persistenceHandler; this.persistenceHandler = persistenceHandler;
this.suppressUpdates = false; this.suppressUpdates = false;
this.closeStrategy = TransactionCloseStrategy.COMMIT; this.closeStrategy = TransactionCloseStrategy.COMMIT;
@ -152,7 +152,7 @@ public class PostgreSqlStrolchTransaction implements StrolchTransaction {
this.txResult.setStartTime(this.startTimeDate); this.txResult.setStartTime(this.startTimeDate);
this.txResult.setTxDuration(txDuration); this.txResult.setTxDuration(txDuration);
this.txResult.setCloseDuration(closeDuration); this.txResult.setCloseDuration(closeDuration);
this.txResult.setRealm(this.realm); 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 was completed after "); //$NON-NLS-1$
@ -201,7 +201,7 @@ public class PostgreSqlStrolchTransaction implements StrolchTransaction {
this.txResult.setStartTime(this.startTimeDate); this.txResult.setStartTime(this.startTimeDate);
this.txResult.setTxDuration(txDuration); this.txResult.setTxDuration(txDuration);
this.txResult.setCloseDuration(closeDuration); this.txResult.setCloseDuration(closeDuration);
this.txResult.setRealm(this.realm); this.txResult.setRealm(getRealm().getRealm());
} }
@Override @Override
@ -233,7 +233,7 @@ public class PostgreSqlStrolchTransaction implements StrolchTransaction {
*/ */
Connection getConnection() { Connection getConnection() {
if (this.connection == null) { if (this.connection == null) {
this.connection = this.persistenceHandler.getConnection(this.realm); this.connection = this.persistenceHandler.getConnection(getRealm().getRealm());
} }
return this.connection; return this.connection;
} }

View File

@ -113,13 +113,13 @@ public class ObserverUpdateTest {
// create order // create order
Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType", new Date(), State.CREATED); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType", new Date(), State.CREATED); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = runtimeMock.getOrderMap().openTx();) { try (StrolchTransaction tx = runtimeMock.getDefaultRealm().openTx();) {
tx.getOrderDao().save(newOrder); tx.getOrderDao().save(newOrder);
} }
// create resource // create resource
Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
try (StrolchTransaction tx = runtimeMock.getResourceMap().openTx();) { try (StrolchTransaction tx = runtimeMock.getDefaultRealm().openTx();) {
tx.getResourceDao().save(newResource); tx.getResourceDao().save(newResource);
} }