You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by cobusstroebel <co...@scss.co.za> on 2019/02/08 06:06:18 UTC

How to provide a transactional entity manager to a third party

I am trying to use the Axon Framework inside Apache Karaf.
During the configuration of the Axon Framework I want to setup a
JpaTokenStore which requires an EntityManager.

Configurer configurer = DefaultConfigurer.defaultConfiguration()
    .configureAggregate(MyAggregate.class)
    .eventProcessing(eventProcessingConfigurer -> {
        eventProcessingConfigurer.registerTokenStore(configuration -> 
            JpaTokenStore.builder()
                .entityManagerProvider(new
SimpleEntityManagerProvider(*entityManager*))
                .serializer(configuration.serializer())
                .build()
        );
    }
);

Whenever I provide an EntityManager I keep getting this error:
java.lang.IllegalStateException: Need active coordination. I know it is
because the injected entity manager is used outside of the class it is
injected into.

One of the guys from Axon suggested that I use a TransactionManager similar
to this one
(https://github.com/AxonFramework/AxonFramework/blob/master/spring/src/main/java/org/axonframework/spring/messaging/unitofwork/SpringTransactionManager.java)
but I don't know how to go about this in Karaf.

Can anyone please nudge me in the right direction?

Kindest Regards
Cobus



--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html

Re: How to provide a transactional entity manager to a third party

Posted by Tim Ward <ti...@paremus.com>.
Hi,

If you want to use a managed EntityManager (rather than one that you create and destroy yourself) then you need to do so within managed scope.

In Transaction Control (which you may have seen emails about on this list in the last few days) this would be by creating a scoped EntityManager from a JPAEntityManagerProvider, and then setting up a scope whenever you access the EntityManager. For example as the integration tests do here <https://github.com/apache/aries-tx-control/blob/48a6b66ea6b15492a97661945b286af176d1204e/tx-control-providers/jpa/tx-control-jpa-itests/src/test/java/org/apache/aries/tx/control/itests/AbstractSimpleTransactionTest.java#L45-L48>.

For a managed EntityManager obtained in another way you will still need to set up the appropriate context to access it. This may be using the Coordinator service (if coordinations are being used for lifecycle management), or it may be by setting up a JTA transaction in some way. 

As you haven’t provided many details about how the EntityManager is set up or maintained it’s hard to be more specific, but I hope this helps to point you in the correct direction.

Tim

> On 8 Feb 2019, at 07:06, cobusstroebel <co...@scss.co.za> wrote:
> 
> I am trying to use the Axon Framework inside Apache Karaf.
> During the configuration of the Axon Framework I want to setup a
> JpaTokenStore which requires an EntityManager.
> 
> Configurer configurer = DefaultConfigurer.defaultConfiguration()
>    .configureAggregate(MyAggregate.class)
>    .eventProcessing(eventProcessingConfigurer -> {
>        eventProcessingConfigurer.registerTokenStore(configuration -> 
>            JpaTokenStore.builder()
>                .entityManagerProvider(new
> SimpleEntityManagerProvider(*entityManager*))
>                .serializer(configuration.serializer())
>                .build()
>        );
>    }
> );
> 
> Whenever I provide an EntityManager I keep getting this error:
> java.lang.IllegalStateException: Need active coordination. I know it is
> because the injected entity manager is used outside of the class it is
> injected into.
> 
> One of the guys from Axon suggested that I use a TransactionManager similar
> to this one
> (https://github.com/AxonFramework/AxonFramework/blob/master/spring/src/main/java/org/axonframework/spring/messaging/unitofwork/SpringTransactionManager.java)
> but I don't know how to go about this in Karaf.
> 
> Can anyone please nudge me in the right direction?
> 
> Kindest Regards
> Cobus
> 
> 
> 
> --
> Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html