Configuring a second JPA Entity Manager Factory

I am using Spring Boot 1.9 with Axon 2.3.2.

I successfully configured Spring to provide a JPA Entity Manager for the Event store, but now wish to have a second JPA entity manager/data source for persisting to the Query database. I appreciate that this is perhaps more a Spring question than an Axon one, but it occurred to me that you guys must have crossed this bridge yourselves, and wondered if you might share some examples configurations.

I use Spring 4 annotations thoughout ie no spring xml config, and after following a number of online spring examples, I am getting errors from spring, whichever way I turn.

I cannot directly post code due to client restrictions, but my working starting point is roughly as follows :

@Configuration
AxonConfigClass {
  @Bean
  public CommandBus commandBus() {...}

  @Bean
  public BeanValidationInterceptor() {...}

  @Bean
  public CommandGateway commandGateway {...}

  @Bean
  public EventStore eventStore() {...}

  @Bean
  public EventBus eventBus() { ...}

  @Bean
  public EventSourcingRepository<MyType>() myTypeRepository {...}

  @Bean
  public EntityManagerProvider entityManagerProvider() {
    return new ContainerManagedEntityProvider();
  }
}

@Configuration
PersistenceConfigClass implements EnvironmentAware {
  @Override
  public setEnvironment (Environment env) {
    this.jpaPropertyResolver = new RelaxedPropertyResolver(env, "spring.jpa");
  }

  @Bean(name = "myPU")
  public LocalEntityManagerFactoryBean configureEntityManagerFactory(Datasource ds, JpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
    emfb.setJpaVendorAdapter(jpaVendorAdapter);
    emfb.setDataSource(ds);
    emfb.setPackagesToScan("org.axonframework.eventstore.jpa");
    return emfb;
  }

  @Bean
  public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
    JpaTransactionManager tm = new JpaTransactionManager();
    tm.setEntityManagerFactory(emf);
    return tm;
  }
}

META-INF/persistence.xml

I am now using JDBI for persisting to our query database - the UI will be using JDBI, and it makes sense for me to reuse the entities required for that.
I have succesfully configured Spring to provide me with a second data source for JDBI use.

If anyone does have a pre exisiting github example of spring configured second JPA data source etc with Axon, I would still be very interested to see how you have done it, but as I have JDBI working now, don't sweat it!

Phil

Hi Phil,

the ContainerManagedEntityManagerProvider uses the default Persitence context. If you have multiple contexts, you may have to implement two providers yourself, one for each context. The name of the persistence context is defined in the PersistenceContext annotation, so it can’t be configured at runtime.

Cheers,

Allard