Provide my own EntityManagerProvider?

In our existing app, we have already got multiple data sources. We want axon to use its own dedicated data source. I knew I need to provide my own EntityManagerProvider bean. The question is:

  • I think I also need to manually code to provide my own EventStorageEngine bean, EmbeddedEventStore bean, JpaSagaStore. Am I right? If so, since all these beans stem from this EntityManagerProvider, why can’t axon auto configure them based on if I have already provided my own implementation? Something like @Primary annotation?
  • What should I put for @PersistenceContext(unitName = “???”) for my EntityManager? I am fine with DomainEventEntry and SnapshotEventEntry. But what else should the PersistenceContext include? Is there an existing unitName that I can use?

Thanks for the help.

Persistence unit is the name you defined in your own persistence xml. Has nothing to do with axon. Is „plain jpa“.

Understand that. The point is if I provide my own entitymanager, how are those axon entity classes (e.g. DomainEventEntry) managed by this new entitymanager?

In AXON auto configuration class, I saw there is annotation like this:
@RegisterDefaultEntities(
  packages = {"org.axonframework.eventhandling.tokenstore", "org.axonframework.modelling.saga.repository.jpa"}
)


Dose that mean I don't need to call EntityManagerFactoryBuilder.packages("") during creation of my own LocalContainerEntityManagerFactoryBean?
All I need is to provide my own entityManagerProvider and those required entities will be registered automatically to the persistent context?

Hi Chun,

As Johnny already pointed out, the specifics of your EntityManager configuration are not that much Axon specific at all.
Providing an EntityManager just for Axon entities, means you need to take the DomainEventEntry, SnapshotEventEntry, TokenEntry, AssociationValueEntry and SagaEntry in to account.
A quick search on the right version in the framework’s repository should give you the correct packages you need to set on it.

When it comes to configuring, this can go two ways, which currently isn’t clear given the issue description:

  1. If you are using Axon’s Configuration API, you can simply use the DefautlConfigurer.jpaConfiguration(EntityManagerProvider, TransactionManager) to set your own EntityManagerProvider bean everywhere you need it.
  2. If you are using the Spring Boot auto configuration you can simply provide an EntityManagerProvider bean. The [JpaAutoConfiguration](https://github.com/AxonFramework/AxonFramework/blob/master/spring-boot-autoconfigure/src/main/java/org/axonframework/springboot/autoconfig/JpaAutoConfiguration.java) and [JpaEventStoreAutoConfiguration](https://github.com/AxonFramework/AxonFramework/blob/master/spring-boot-autoconfigure/src/main/java/org/axonframework/springboot/autoconfig/JpaEventStoreAutoConfiguration.java) will pick this up automatically and set it for you.

Thus, based on your “why can’t axon auto configure them” statement, I would assume something else is amiss in your configuration if the above shared doesn’t solve it.

Hope this helps Chun, nice to see you back on the forum.

Cheers,
Steven

Hi Steve,

Long time no talk. Thanks for the reply. I did manage to get it working. Just have to list all the axon packages involved when configuring entity manager.

Hi Chun,

Happy to hear you had it resolved in the end!

Cheers,
Steven