Axon Framework - How can i rollback a Saga process

polosoft :

I am using the Axon Framework without the Axon Server with spring-boot auto config. My question is: If i am running a saga process as you can see below, and in the middle in processing i run out of disk, there will be an exception, and i have to rollback. How can I do that? How much i have to roll back?

My saga_entry table will be this:

SAGA_ID: 22ad255b-4378-4bb4-84c2-061ca666c6e7, REVISION: null, SAGA_TYPE: hu.saga.account.SagaAccount, SERIALIZED_SAGA: 3c68752e6d6f6c617269732e736167612e6163636f756e742e536167614163636f756e742f3e

My association_value_entry table will be this:

ID: 2, ASSOCIATION_KEY: accountId, ASSOCIATION_VALUE: 11, SAGA_ID: 22ad255b-4378-4bb4-84c2-061ca666c6e7, SAGA_TYPE: hu.saga.account.SagaAccount

And in my domain_event_entry table will be only two events (not 3 as it should be) AccountCreatedEvent, MoneyWithdrawnEvent with the aggregate id: 11 .

This is just an example, may main question is what is the best way to handle and implement a rollback in Axon Saga?

@StartSaga
@SagaEventHandler(associationProperty = "accountId")
public void handle(AccountCreatedEvent event){
    logger.info("Saga Start");
    commandGateway.send(new WithdrawMoneyCommand(event.getAccountId(), event.getAccountId(),event.getOverDraftLimit()));
}

I write null to imitate a potential rollback in the DepositMoneyCommand constructor

@SagaEventHandler(associationProperty = "accountId")
public void handle(MoneyWithdrawnEvent event){
    commandGateway.send(new DepositMoneyCommand(null,event.getTransactionId(),event.getAmount()));
}


@EndSaga
@SagaEventHandler(associationProperty = "accountId")
public void handle(MoneyDepositedEvent event){
    logger.info("Saga End");
}

I mean if i dont handle it the saga tables will not be empty as it should be, and i cant fire an other saga for that aggregate id: 11, because its already exists in the domain_event-entry table

How can I delete from the domain_event_entry table? AggregateLifecycle.markdeleted() not working for me.

Steven :

I like to start off with noting that a Saga models a single instance of a Complex Business Transaction. Axon provides quite some documentation on the topic, which you can read here.

Then moving over to your Saga snippet, it seems you are starting a Saga snippet on the start of an Account Aggregate. I would argue this is not the right initiation point of the Saga at all from the Account/Wallet perspective I assume you are in. Instead, your Saga should model a single (complex business) transaction, thus just the Withdraw or Deposit.

AxonIQ and Pivotal partnered up last year to provide just such a sample project using a Saga to this end, the Axon Trader. I'd suggest you check out this project for further insights in how to deal with a Saga in general.

By the way, to answer the title question: you will not personally rollback a Saga, ever. If there's an exceptional state, Axon's Unit of Work concept will kick-in the rollback phase to revert some operations.

If something should be rolledback outside the concept of an exceptional state, then you should deal with this yourself. This is necessary as your Saga models the entire transaction, thus also the faulty states. You'd typically dispatch compensating action (e.g. commands) as a reaction to faulty states.

Hope this clarifies somethings for you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=142142&siteId=1