Store sagas in JpaSagaRepository or MongoSagaRepository

Hi,

I can see that there are three options for persisting sagas, either InMemorySagaRepository, JpaSagaRepository or MongoSagaRepository. Since I’ve chosen to persist the events in a MongoDB, I also assume it would be best to store the sagas in the same DB (less to configure, less to maintain etc…). I’ve searched various examples on how to setup the Spring configuration for a MongoSagaRepository, but I’m unable to complete it:(

I paste what I’ve got so far, and hope someone can help me out with the rest:

<axon:saga-manager id=“mySagaManager” saga-repository=“mongoSagaRepository” event-bus=“eventBus”>
<axon:async processor-count=“10” executor=“myThreadPool” transaction-manager=“txManager”/>
axon:types
com.foo.MySaga
</axon:types>
</axon:saga-manager>

// I assume this bean is the one causing problems. According to the JavaDoc, I need to set a PlatformTransactionManager using setter injection, but I don’t know to what I should set it! (http://www.axonframework.org/apidocs/2.1/org/axonframework/unitofwork/SpringTransactionManager.html)
// Currently it results in a NullPointerException on line 67 (return transactionManager.getTransaction(transactionDefinition);

<axon:event-bus id=“eventBus”/>

<task:executor id=“myThreadPool” pool-size=“30” />

Hi Viggo,

you don’t need to use a TransactionManager at all. Mongo doesn’t support transactions. When using e.g. JPA, you can set Spring’s PlatformTransactionManager there.

Removing the tx manager should solve your problem.
Cheers,

Allard

Excellent, thanks Allard, it worked:)

Follow-up question on the Saga: It seems Spring doesn’t inject the CommandGateway for me, since I get a NullPointerException when building my project. I have my setter for the commandgateway like this:

private transient CommandGateway commandGateway;


@StartSaga
@SagaEventHandler(associationProperty = “purchaseOrderId”)
public void handle(TransportOrderSentEvent event) {
supplierOrderId = “supplier-order-id-” + UUID.randomUUID().toString(); // TODO: Refactor into making GSI-compliant identifiers
description = “supplier-description-” + UUID.randomUUID().toString(); // TODO: Generate meaningful description

log.debug(“L1 - Starting SupplierSaga for purchaseOrderId '” + event.getPurchaseOrderId() + “’ -> '” + supplierOrderId + “’ (supplierOrderId)”);

this.purchaseOrderId = event.getPurchaseOrderId(); // keep track of the purchaseOrderId for this supplierOrderId (should always match)

commandGateway.send( new CreateSupplierOrderCommand(supplierOrderId, description)); // NullPointerException thrown when building my project
}


@Resource
public void setCommandGateway(CommandGateway commandGateway) {
this.commandGateway = commandGateway;
}

Hi Viggo,

for resources to be injected, you need to specify a “ResourceInjector”. Since you’re using Spring, you’d most likely want to use the SpringResourceInjector. It will use Spring’s injection mechanism to autowire beans.

Unfortunately, there is no axon-mongo namespace support yet. That could automatically take care of all this.

Cheers,

Allard

Hi,
have you any idea how can I handle it? I tried add setters, but I have NPE all the time…

Cheers,
Szymek

Hi Szymek,

Your spring config for the mongo saga repository is missing a SpringResourceInjector. Eg change to:

You will probably also want to use another serializer than the default JavaSerializer. If so you need to set the serializer property too.

Rene

Yeap, that’s it. :slight_smile:

Thanks,
Szymek