Configuring a second JPA Entity Manager Factory

987 views
Skip to first unread message

Phil Whiles

unread,
Nov 17, 2014, 6:19:41 AM11/17/14
to axonfr...@googlegroups.com
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
-------------------------------------
//SNIP
<persistence-unit name="myPU" transaction-type="RESOURCE-LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.axonframework.eventstore.jpa.DomainEntry</class>
<class>org.axonframework.eventstore.jpa.DomainEntry</class>
</provider>

application.yml
--------------------
spring:
datasource:
driverClassName: org.h2.Driver
user: sa
password: sa
url: jdbc:h2:tcp://localhost:10402/eventstore
jpa:
database-platform: org.hibernate.dialect.H2Dialect
generate-ddl: true
show-sql: false


This works, and I arrived at this config through a lot of trial and error, and Axon works fine.

My problem is with Spring, once I try and introduce multiple data sources and entity manager factories, as per the Spring online docs.
It seems that Spring is still trying to create default data source/entity managers for me, and ends up trying to set up connections to the correct db url, but without username/password.

Bottom line - does anyone have a working @ config setup they could share that shows how to setup axon with spring boot to use JPA for its event store, while also using a second JPA data source for its persistence to the Query database?

Phil

Phil Whiles

unread,
Nov 17, 2014, 10:52:33 AM11/17/14
to axonfr...@googlegroups.com
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

Allard Buijze

unread,
Nov 21, 2014, 4:43:16 PM11/21/14
to axonfr...@googlegroups.com
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




--
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.


Reply all
Reply to author
Forward
0 new messages