EntityManager transaction error even though handler interceptors are configured on command bus and eventhandling configuration

59 views
Skip to first unread message

Christopher Usick

unread,
Jan 2, 2018, 2:49:38 PM1/2/18
to Axon Framework Users
I'm using axon with spring boot with H2 for database. I'm getting the following error when axon tries to persist an event using the JpaEventStorageEngine:

2018-01-02 11:02:05.023  WARN 31806 --- [ol-124-thread-1] o.a.c.gateway.DefaultCommandGateway      : Command 'com.galaxy.poc.commands.CreatePostCommand' resulted in org.axonframework.eventsourcing.eventstore.EventStoreException(An event with identifier [8121256c-d118-4ce4-a5b9-e975e90f942b] could not be persisted)
2018-01-02 11:02:05.044 ERROR 31806 --- [ol-124-thread-1] o.s.w.s.adapter.HttpWebHandlerAdapter    : Failed to handle request
org.axonframework.eventsourcing.eventstore.EventStoreException: An event with identifier [8121256c-d118-4ce4-a5b9-e975e90f942b] could not be persisted
at org.axonframework.eventsourcing.eventstore.AbstractEventStorageEngine.handlePersistenceException(AbstractEventStorageEngine.java:139) ~[axon-core-3.1.jar:3.1]
at org.axonframework.eventsourcing.eventstore.jpa.JpaEventStorageEngine.appendEvents(JpaEventStorageEngine.java:365) ~[axon-core-3.1.jar:3.1]
at org.axonframework.eventsourcing.eventstore.AbstractEventStorageEngine.appendEvents(AbstractEventStorageEngine.java:112) ~[axon-core-3.1.jar:3.1]
at org.axonframework.eventsourcing.eventstore.AbstractEventStore.prepareCommit(AbstractEventStore.java:64) ~[axon-core-3.1.jar:3.1]
at org.axonframework.eventhandling.AbstractEventBus.doWithEvents(AbstractEventBus.java:209) ~[axon-core-3.1.jar:3.1]
at org.axonframework.eventhandling.AbstractEventBus.lambda$null$4(AbstractEventBus.java:144) ~[axon-core-3.1.jar:3.1]
at org.axonframework.messaging.unitofwork.MessageProcessingContext.notifyHandlers(MessageProcessingContext.java:68) ~[axon-core-3.1.jar:3.1]
at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.notifyHandlers(DefaultUnitOfWork.java:91) ~[axon-core-3.1.jar:3.1]
at org.axonframework.messaging.unitofwork.AbstractUnitOfWork.changePhase(AbstractUnitOfWork.java:221) ~[axon-core-3.1.jar:3.1]
at org.axonframework.messaging.unitofwork.AbstractUnitOfWork.commitAsRoot(AbstractUnitOfWork.java:82) ~[axon-core-3.1.jar:3.1]
at org.axonframework.messaging.unitofwork.AbstractUnitOfWork.commit(AbstractUnitOfWork.java:70) ~[axon-core-3.1.jar:3.1]
at org.axonframework.messaging.unitofwork.DefaultUnitOfWork.executeWithResult(DefaultUnitOfWork.java:80) ~[axon-core-3.1.jar:3.1]
at org.axonframework.commandhandling.SimpleCommandBus.handle(SimpleCommandBus.java:156) ~[axon-core-3.1.jar:3.1]
at org.axonframework.commandhandling.AsynchronousCommandBus.lambda$handle$1(AsynchronousCommandBus.java:83) ~[axon-core-3.1.jar:3.1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_112]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_112]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_112]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Assembly trace from producer [reactor.core.publisher.MonoCompletionStage] :
reactor.core.publisher.Mono.fromFuture(Mono.java:388)
com.galaxy.poc.handlers.ApiHandler.postPost(ApiHandler.java:57)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
... (stack omitted for brevity)
Caused by: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:288) ~[spring-orm-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at com.sun.proxy.$Proxy750.persist(Unknown Source) ~[na:na]
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) ~[na:1.8.0_112]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_112]
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374) ~[na:1.8.0_112]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_112]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_112]
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) ~[na:1.8.0_112]
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) ~[na:1.8.0_112]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_112]
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) ~[na:1.8.0_112]
at org.axonframework.eventsourcing.eventstore.jpa.JpaEventStorageEngine.appendEvents(JpaEventStorageEngine.java:360) ~[axon-core-3.1.jar:3.1]
... 15 common frames omitted

I am adding a `TransactionManagingInterceptor` to the asynchronous command bus and the event processors with the following config: 

@Configuration
@EnableWebFlux
@ComponentScan("com.galaxy.common")
@EnableWebFluxSecurity
class AppConfig {
    @Bean
    RouterFunction<?> mainRouterFunction(@Autowired ApiHandler apiHandler, @Autowired ErrorHandler errorHandler) {
        return MainRouter.doRoute(apiHandler, errorHandler);
    }

    @Bean
    protected SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) throws Exception {
        http.authorizeExchange().anyExchange().permitAll();
        http.csrf().disable();
        return http.build();
    }

    @Bean
    protected CommandBus asyncronousCommandBus(TransactionManager transactionManager) {
        SimpleCommandBus commandBus = new AsynchronousCommandBus();
        commandBus.registerHandlerInterceptor(new TransactionManagingInterceptor<>(transactionManager));

        return new AsynchronousCommandBus();
    }

    @Autowired
    public void configure(EventHandlingConfiguration eventHandlingConfiguration, TransactionManager transactionManager) {
        eventHandlingConfiguration.usingTrackingProcessors();
        eventHandlingConfiguration.registerHandlerInterceptor("com.galaxy.poc.listeners", (config) -> {
            return new TransactionManagingInterceptor<>(transactionManager);
        });
    }
}

Is there another area where I need to configure the transaction managing interceptor? I have also tried adding `org.springframework.transaction.annotation.Transactional` annotation to the Aggregate and the listener which didn't change the result.

Thanks for the help,

Chris


Allard Buijze

unread,
Jan 3, 2018, 3:07:25 AM1/3/18
to axonfr...@googlegroups.com
Hi,

in your @Bean definition, you're creating a new AsynchronousCommandBus and assign a CommandHandlingInterceptor to it, which is the way to go. However, on the next line, you don't return the initialized instance, but a fresh one. Just return "commandBus", and all your problems should disappear.

Cheers,

Allard


Op di 2 jan. 2018 om 20:49 schreef Christopher Usick <christop...@gmail.com>:
--
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.


--

Christopher Usick

unread,
Jan 3, 2018, 10:57:52 AM1/3/18
to Axon Framework Users
Of course! stupid mistake, thanks for the help!
Reply all
Reply to author
Forward
0 new messages