Implementing CQRS with Axon : Multiple datasources and transaction managers

434 views
Skip to first unread message

Ranjit Nair

unread,
Dec 14, 2015, 1:15:23 AM12/14/15
to Axon Framework Users
Hi,
   I am new to Axon framework and working on an application using the framework. Following the CQRS tenants I would like to separate the Command and Query parts, the following is what I have implemented ,
 
Command App (DataStore A)
-------------------
- Created a Hibernate Repository by implementing the Axon AbstractRepository
- The above hibernate repository uses "org.springframework.orm.hibernate5.HibernateTransactionManager'
- The Transaction manager is set to the Command bus using the axon SpringTransactionManager
- The aggregates in the domain generate events when the states change
 
Query App (Data Store B)
------------------
- For this I used the Spring data JPA infrastructure, hence the "org.springframework.orm.jpa.JpaTransactionManager' had to be used.
- This app has event listeners listening to events generated from the Command App
- On receipt of the event it converts into a query specific entity and persists in the DB using the Spring Data Repository.
 
Note : Command and Query App are in the same JVM
 
Problem Statement
-------------------------
- The UnitOfWork is only aware of the transaction manager in the Command App, hence only the state of the aggregate is getting persisted in the Command DB, whereas the entities are not persisting
in Query App
 
Am I doing something wrong here or is my idea of CQRS flawed? I don't want to use Event Sourcing at this poing of time since I am new to Axon framework.
 
Any help will be greatly appreciated.
 
Thanks,
Ranjit

Allard Buijze

unread,
Dec 14, 2015, 10:37:20 AM12/14/15
to Axon Framework Users
Hi Ranjit,

which EventBus implementation do you use?

With the SimpleEventBus, events are handled in the same transaction as the one that handles the command. If you query component requires another transaction for persisting the results in another store, you need to register a unit of work listener with the unit of work, to commit a transaction for the new data store during the commit phase.

Cheers,

Allard

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ranjit Nair

unread,
Dec 14, 2015, 1:05:23 PM12/14/15
to Axon Framework Users
Hi Allard,
        I use the SimpleEventBus. Thanks for your suggestion regarding the use of unit of work listener. What is the mechanism for registering the unit of work listeners with the DefaulUnitOfWorkFactory? Do I need to implement my own UnitOfWorkFactory to attach the listeners to the UnitOfWork? I use spring to wire up this components.

Regards,
Ranjit N

Allard Buijze

unread,
Dec 30, 2015, 1:58:27 PM12/30/15
to Axon Framework Users
Hi Ranjit,

you can always register listeners with the current Unit of Work by accessing it though the static CurrentUnitOfWork.get() method. It will throw an exception when no UnitOfWork is active, so be sure to check its existence using CurrentUnitOfWork.isStarted(). With the UnitOfWork reference, you can simply register a listener.

Cheers,

Allard
Reply all
Reply to author
Forward
0 new messages