Skip to content

Commit

Permalink
Merge pull request #31 from codeliner/hotfix/nested-transaction-support
Browse files Browse the repository at this point in the history
Hotfix/nested transaction support
  • Loading branch information
codeliner committed Jun 24, 2015
2 parents c65e56a + 58dae7c commit d064dd6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 41 deletions.
32 changes: 5 additions & 27 deletions spec/EventStore/TransactionManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,7 @@ function it_does_not_perform_a_rollback_when_command_implements_auto_commit_comm

$this->onFinalize($commandDispatch);
}

function it_does_not_perform_a_rollback_when_it_is_not_in_transaction(EventStore $eventStore, CommandDispatch $commandDispatch)
{
$eventStore->rollback()->shouldNotBeCalled();

$commandDispatch->getException()->willReturn(new \Exception());

$this->onFinalize($commandDispatch);
}


function it_commits_the_transaction_on_finalize(EventStore $eventStore, CommandDispatch $commandDispatch)
{
$eventStore->beginTransaction()->shouldBeCalled();
Expand All @@ -109,20 +100,15 @@ function it_commits_the_transaction_on_finalize(EventStore $eventStore, CommandD
$this->onFinalize($commandDispatch);
}

function it_handles_nested_transactions_by_invoking_event_store_commit_only_when_all_commands_are_dispatched(EventStore $eventStore, CommandDispatch $commandDispatch)
function it_handles_nested_transactions(EventStore $eventStore, CommandDispatch $commandDispatch)
{
$eventStore->beginTransaction()->shouldBeCalled();
$eventStore->beginTransaction()->shouldBeCalledTimes(2);
$eventStore->commit()->shouldBeCalledTimes(2);
$commandDispatch->getException()->shouldBeCalled();

$this->onInitialize($commandDispatch);
$this->onInitialize($commandDispatch);

$eventStore->commit()->shouldNotBeCalled();
$commandDispatch->getException()->shouldBeCalled();

$this->onFinalize($commandDispatch);

$eventStore->commit()->shouldBeCalled();

$this->onFinalize($commandDispatch);
}

Expand All @@ -140,14 +126,6 @@ function it_does_not_commit_transaction_when_command_implements_auto_commit_comm
$this->onFinalize($commandDispatch);
}

function it_does_not_commit_transaction_when_it_is_not_in_transaction(EventStore $eventStore, CommandDispatch $commandDispatch)
{
$commandDispatch->getException()->shouldBeCalled();
$eventStore->commit()->shouldNotBeCalled();

$this->onFinalize($commandDispatch);
}

function it_adds_meta_information_about_command_to_each_recorded_event_of_a_new_aggregate_root(
EventStore $eventStore, ActionEvent $actionEvent, Stream $eventStream,
CommandDispatch $commandDispatch, Command $command, EventBus $eventBus)
Expand Down
16 changes: 2 additions & 14 deletions src/EventStore/TransactionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ final class TransactionManager implements ActionEventListenerAggregate
*/
private $eventStore;

private $transactionCount = 0;

/**
* @var Command
*/
Expand Down Expand Up @@ -120,22 +118,16 @@ public function onInitialize(CommandDispatch $commandDispatch)
$command = $commandDispatch->getCommand();

if ($command instanceof Command && !$command instanceof AutoCommitCommand) {
if ($this->transactionCount === 0) {
$this->eventStore->beginTransaction();
}

$this->transactionCount++;
$this->eventStore->beginTransaction();
$this->currentCommand = $command;
}
}

public function onError(CommandDispatch $commandDispatch)
{
if (! $commandDispatch->getCommand() instanceof Command || $commandDispatch->getCommand() instanceof AutoCommitCommand) return;
if ($this->transactionCount === 0) return;

$this->eventStore->rollback();
$this->transactionCount = 0;
$this->currentCommand = null;
}

Expand All @@ -147,12 +139,8 @@ public function onFinalize(CommandDispatch $commandDispatch)
}

if (! $commandDispatch->getCommand() instanceof Command || $commandDispatch->getCommand() instanceof AutoCommitCommand) return;
if ($this->transactionCount === 0) return;

if ($this->transactionCount === 1) {
$this->eventStore->commit();
}
$this->transactionCount--;
$this->eventStore->commit();
$this->currentCommand = null;
}

Expand Down

0 comments on commit d064dd6

Please sign in to comment.