AxonFramework / AxonFramework

Framework for Evolutionary Message-Driven Microservices on the JVM
https://axoniq.io/
Apache License 2.0
3.33k stars 790 forks source link

process event fail #2000

Closed cjdxhjj closed 2 years ago

cjdxhjj commented 2 years ago

Basic information

Steps to reproduce

Expected behaviour

process event success

Actual behaviour

2021-11-09 10:45:29,599 [EventProcessor[GoodsSkuUpdateSagaProcessor]-0] INFO [product-center] TrackingEventProcessor Release claim failed org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:467) at org.springframework.transaction.support.AbstractPlatformTransactionManager.startTransaction(AbstractPlatformTransactionManager.java:400) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) at org.axonframework.spring.messaging.unitofwork.SpringTransactionManager.startTransaction(SpringTransactionManager.java:59) at org.axonframework.common.transaction.TransactionManager.executeInTransaction(TransactionManager.java:45) at org.axonframework.eventhandling.TrackingEventProcessor.releaseToken(TrackingEventProcessor.java:348) at org.axonframework.eventhandling.TrackingEventProcessor.processingLoop(TrackingEventProcessor.java:321) at org.axonframework.eventhandling.TrackingEventProcessor$TrackingSegmentWorker.run(TrackingEventProcessor.java:1005) at org.axonframework.eventhandling.TrackingEventProcessor$WorkerLauncher.run(TrackingEventProcessor.java:1151) at java.base/java.lang.Thread.run(Thread.java:834) Caused by: java.lang.IllegalStateException: EntityManagerFactory is closed at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:530) at org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:609) at org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:156) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.createNativeEntityManager(AbstractEntityManagerFactoryBean.java:585) at jdk.internal.reflect.GeneratedMethodAccessor141.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:487) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:734) at com.sun.proxy.$Proxy144.createNativeEntityManager(Unknown Source) at org.springframework.orm.jpa.JpaTransactionManager.createEntityManagerForTransaction(JpaTransactionManager.java:485) at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:410) ... 9 common frames omitted

smcvb commented 2 years ago

As you can spot from the stack trace, @cjdxhjj, the issue resides with the EntityManagerFactory. Since the EntityManagerFactory is part of your application's configuration, there's not that much we can do on the Axon Framework side of thing. So, check out how you're configuration your entity manager for your project.

Note that from Axon's perspective, it's correctly wired, as otherwise, the TrackingEventProcessor wouldn't try to start a transaction.

Since Axon's repositories aren't intended for questions like these, I will close this issue with the label 'question'.

cjdxhjj commented 2 years ago

thanks, it prove that the EntityManagerFactory affect by a mistake rabbitmq confiuration, that cause the EntityManagerFactory not working, i guess the mq configuration error cause the mq retry time to time, and hang the transaction a very long time, exceed the max time of lifecycle of EntityManagerFactory, when i fix the mq configuration, then the event processing works, but. other sense, for example, when the business logic is slow, it may cause the same issure.

smcvb commented 2 years ago

It's great to hear you spotted the issue with your RabbitMQ configuration!

other sense, for example, when the business logic is slow, it may cause the same issue.

If I follow you correctly, you're expressing concerns that long-running event handling transactions would preemptively close your EntityManagerFactory. Interestingly enough, I have never seen this being an issue. Furthermore, your code controls how long the event handling task takes as you write the @EventHandler annotated methods. To give an idea, Axon Framework would, within an event handling transaction, perform the following tasks:

Axon's processing time is very minimal when it comes to retrieval of events and tokens, as well as for updating the token. So whether your timeout is exceeded very much depends on your implementation.

This situation thus warrants validating what the ideal transaction timeout is. However, what the ideal number is for the transaction time is something you'd have to figure out yourself, I'm afraid.

cjdxhjj commented 2 years ago

thanks very much,you are rigth, the event process may as soon as possible, in fact, my event processer are async, i use the r2dbc, zio. i can always found a right way not to block it. the only troublesome is that the saga event handler can't run async mode