Persistence context with multiple persistence units

504 views
Skip to first unread message

Yannis Gonianakis

unread,
Nov 7, 2011, 6:09:43 AM11/7/11
to axonfr...@googlegroups.com
Hello everyone,

This is my first post in the group and with this opportunity I want to thank you for your great work in Axon framework.

The problem I'm facing is quite simple but I don't know if there is a built-in way to tackle it.
In my application I have a persistence context configuration with more than one persistence units. When I try to instantiate JpaEventStore in my Spring context I get the following error: 

Error creating bean with name 'eventStore': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 3

This error is caused due to the unqualified annotation @PersistenceContext found on setEntityManager in org.axonframework.eventstore.jpa.JpaEventStore. 

In addition, extending JpaEventStore and overriding setEntityManager doesn't seem to work.
public class MyJpaEventStore extends JpaEventStore {
    @PersistenceContext(unitName = "eventStorePersistenceUnit")
    public void setEntityManager(EntityManager em) {
        super.setEntityManager(em);
    }
}

What would be the best way to deal with this scenario? Is it axon's responsibility to inject the entityManager in the first place?

Cheers,
Yannis

Domenico Maria Giffone

unread,
Nov 7, 2011, 7:10:44 AM11/7/11
to axonfr...@googlegroups.com
Hi Yannis,

the problem seems to depend more on spring pu configuration rather than on axon however try to see if this link is of some help:
Keeping your implementation of JpaEventStore, try to apply the configuration suggested in the link above and configure the event store as described here: http://www.axonframework.org/docs/1.2/using-spring.html? # D4e1565 without using Axon namespace support.
Cheers,

Domenico

Allard Buijze

unread,
Nov 7, 2011, 7:37:21 AM11/7/11
to axonfr...@googlegroups.com
Hi Yannis, Domenico,

I have to admit there is something in the implementation of the JpaEventStore that doesn't make it very suitable when using multiple persistence units. It has to do with the fact that the EventStore has a setter for the EntityManager with an @PersistenceContext annotation on it.

That works fine in most Spring/EE containers, when using a single PU. Spring Data has a PersistenceUnitManager that allows you to reconfigure multiple PU's as a single one. A project here at Dutchworks used that as workaround.

Perhaps I need to extract the EntityManager into an "EntityManagerProvider", which defaults to one that uses the default EntityManager, like the current implementation does. If you need to inject another EntityManager, or really need an EntityManager per transaction (which is the pure approach), a more specific implementation could be provided.

I could schedule such a solution for 1.3. For now, perhaps Domenico's solution will work for you.

Cheers,

Allard

Allard Buijze

unread,
Nov 7, 2011, 9:14:51 AM11/7/11
to axonfr...@googlegroups.com
I have created issue #217 (http://code.google.com/p/axonframework/issues/detail?id=217) to solve this issue.

Yannis Gonianakis

unread,
Nov 7, 2011, 9:46:58 AM11/7/11
to axonfr...@googlegroups.com
Hi guys,

thank you both for your prompt replies. 

I tried to implement a solution based on Domenico's suggestions but I couldn't make it in the end.
As Allard mentioned, the problem lies with the unqualified @PersistenceContext inside JpaEventStore. Any annotated fields or methods with @PersistenceUnit or @PersistenceContext annotations in a Spring-managed object will automatically be injected and since I have more than one eligible candidates for injection the container doesn't know which one to use. 

Nevertheless, I'm really thankful that you will address this issue in v1.3 and in the meantime I will just go for the quick and dirty solution i.e. override JpaEventStore class in the classpath and qualify the annotation using unitName (@PersistenceContext(unitName = "eventStorePersistenceUnit")). 

Cheers, 
Yannis
Reply all
Reply to author
Forward
0 new messages