[Devel] fixing flushing of TX not working on rollback
This commit is contained in:
parent
b0cf516dab
commit
a9f3b04b3f
|
@ -89,6 +89,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
private TransactionResult txResult;
|
private TransactionResult txResult;
|
||||||
|
|
||||||
private List<Command> commands;
|
private List<Command> commands;
|
||||||
|
private List<Command> flushedCommands;
|
||||||
private Set<StrolchRootElement> lockedElements;
|
private Set<StrolchRootElement> lockedElements;
|
||||||
|
|
||||||
private AuditingOrderMap orderMap;
|
private AuditingOrderMap orderMap;
|
||||||
|
@ -112,6 +113,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
this.certificate = certificate;
|
this.certificate = certificate;
|
||||||
|
|
||||||
this.commands = new ArrayList<>();
|
this.commands = new ArrayList<>();
|
||||||
|
this.flushedCommands = new ArrayList<>();
|
||||||
this.lockedElements = new HashSet<>();
|
this.lockedElements = new HashSet<>();
|
||||||
this.closeStrategy = TransactionCloseStrategy.ROLLBACK;
|
this.closeStrategy = TransactionCloseStrategy.ROLLBACK;
|
||||||
this.txResult = new TransactionResult(getRealmName(), System.nanoTime(), new Date());
|
this.txResult = new TransactionResult(getRealmName(), System.nanoTime(), new Date());
|
||||||
|
@ -470,7 +472,6 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
validateCommands();
|
validateCommands();
|
||||||
doCommands();
|
doCommands();
|
||||||
writeChanges(this.txResult);
|
writeChanges(this.txResult);
|
||||||
this.commands.clear();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
this.closeStrategy = TransactionCloseStrategy.ROLLBACK;
|
this.closeStrategy = TransactionCloseStrategy.ROLLBACK;
|
||||||
|
|
||||||
|
@ -773,8 +774,12 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
* so chance of a runtime exception should be small
|
* so chance of a runtime exception should be small
|
||||||
*/
|
*/
|
||||||
private void doCommands() {
|
private void doCommands() {
|
||||||
for (Command command : this.commands) {
|
ListIterator<Command> iter = this.commands.listIterator();
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
Command command = iter.next();
|
||||||
command.doCommand();
|
command.doCommand();
|
||||||
|
this.flushedCommands.add(command);
|
||||||
|
iter.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -783,8 +788,11 @@ public abstract class AbstractTransaction implements StrolchTransaction {
|
||||||
* performing the commands
|
* performing the commands
|
||||||
*/
|
*/
|
||||||
private void undoCommands() {
|
private void undoCommands() {
|
||||||
for (Command command : this.commands) {
|
ListIterator<Command> iter = this.flushedCommands.listIterator();
|
||||||
|
while (iter.hasPrevious()) {
|
||||||
|
Command command = iter.previous();
|
||||||
command.undo();
|
command.undo();
|
||||||
|
iter.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import li.strolch.command.RemoveResourceCommand;
|
||||||
import li.strolch.model.ModelGenerator;
|
import li.strolch.model.ModelGenerator;
|
||||||
import li.strolch.model.Resource;
|
import li.strolch.model.Resource;
|
||||||
import li.strolch.persistence.api.StrolchTransaction;
|
import li.strolch.persistence.api.StrolchTransaction;
|
||||||
import li.strolch.persistence.api.TransactionCloseStrategy;
|
|
||||||
import li.strolch.service.api.AbstractService;
|
import li.strolch.service.api.AbstractService;
|
||||||
import li.strolch.service.api.ServiceArgument;
|
import li.strolch.service.api.ServiceArgument;
|
||||||
import li.strolch.service.api.ServiceResult;
|
import li.strolch.service.api.ServiceResult;
|
||||||
|
@ -58,6 +57,8 @@ public class FlushTxTest extends AbstractRealmServiceTest {
|
||||||
tx.addCommand(rmResCmd);
|
tx.addCommand(rmResCmd);
|
||||||
tx.flush();
|
tx.flush();
|
||||||
DBC.PRE.assertNull("Expect to remove resource with id " + id, tx.getResourceBy(id, id));
|
DBC.PRE.assertNull("Expect to remove resource with id " + id, tx.getResourceBy(id, id));
|
||||||
|
|
||||||
|
tx.commitOnClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ServiceResult.success();
|
return ServiceResult.success();
|
||||||
|
@ -89,13 +90,16 @@ public class FlushTxTest extends AbstractRealmServiceTest {
|
||||||
DBC.PRE.assertNotNull("Expected resource with id " + id, tx.getResourceBy(id, id));
|
DBC.PRE.assertNotNull("Expected resource with id " + id, tx.getResourceBy(id, id));
|
||||||
|
|
||||||
// now force a rollback
|
// now force a rollback
|
||||||
tx.setCloseStrategy(TransactionCloseStrategy.ROLLBACK);
|
tx.rollbackOnClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
// now make sure the new resource does not exist
|
// now make sure the new resource does not exist
|
||||||
try (StrolchTransaction tx = openTx(arg.realm)) {
|
try (StrolchTransaction tx = openTx(arg.realm)) {
|
||||||
DBC.PRE.assertNull("Did not expect resource with id after rolling back previous TX " + id,
|
|
||||||
tx.getResourceBy(id, id));
|
Resource res = tx.getResourceBy(id, id);
|
||||||
|
if (res != null) {
|
||||||
|
throw tx.fail("Did not expect resource with id after rolling back previous TX " + id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ServiceResult.success();
|
return ServiceResult.success();
|
||||||
|
|
Loading…
Reference in New Issue