Transactionality

My application is configured to use a JPA backed EventSourced repository on the command side, and an external event handler uses JDBI with a second data source to insert corresponding data into the query side db. In blue sky operation everything is working very nicely with my client able to do the full round trip - query from the query db, send commands to my Axon 'core' through a restful interface - the resulting Events are stored with JPA, and events result in inserts to the query db, which the UI client can re-read etc etc

I am examining how/where the application handles transactions - I have created an Axon SpringTransactionManager (itself backed by a JPA PlatformTransactionManager), and injected that transaction manager into my command bus. My application is currently structured such that each command results in a single Event, for better or worse.

Is it possible to include the insert to the query side database inside the transaction that the EventStore will be using?

Is it generally desirable to do so?

Any and all guidance on transactions and Axon greatly appreciated!

Phil

Hi Phil,

all depends on what you expect of your application. If you use CQRS because you want to be able to scale the read and write side independently, you don’t want to rely on a single transaction spanning both. If you don’t care about scaling, it’s possible.

The transaction started by the transaction manager on the CommandBus spans the storage of the aggregate (or its events in the case of event sourcing) as well as the publication of the events. If you use an Event Bus implementation that publishes directly (in the same thread), such as the SimpleCommandBus, you use that same transaction to handle events as well.

Once you start handling events asynchronously, you’ll have to rely on those handlers to work with their own transactions. That’s why I never recommend having you code rely on event handlers to run in the same transaction.

Cheers,

Allard