asc-lab / micronaut-microservices-poc

Very simplified insurance sales system made in a microservices architecture using Micronaut
https://altkomsoftware.pl/en/blog/microservices-micronaut/
Apache License 2.0
490 stars 171 forks source link

[documents-service] org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread. #53

Closed kingzevin closed 4 years ago

kingzevin commented 4 years ago

In documents-service, when a policy is paid, an error is raised: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread. I've added the policy template in JSReport and committed.

14:28:54.569 [pool-2-thread-1] ERROR i.m.c.k.e.KafkaListenerExceptionHandler - Error processing record [Optional[ConsumerRecord(topic = policy-registered, partition = 0, leaderEpoch = null, offset = 3, CreateTime = 1598884133749, serialized key size = 36, serialized value size = 159, headers = RecordHeaders(headers = [], isReadOnly = false), key = [B@7dbe50be, value = pl.altkom.asc.lab.micronaut.poc.policy.service.api.v1.events.PolicyRegisteredEvent@7c3b26a3)]] for Kafka consumer [pl.altkom.asc.lab.micronaut.poc.documents.domain.PolicyRegisteredListener@3a66e52c] produced error: Could not obtain transaction-synchronized Session for current thread
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    at io.micronaut.transaction.hibernate5.MicronautSessionContext.currentSession(MicronautSessionContext.java:100)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:475)
    at io.micronaut.configuration.hibernate.jpa.TransactionalSessionInterceptor.intercept(TransactionalSessionInterceptor.java:56)
    at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:69)
    at io.micronaut.configuration.hibernate.jpa.TransactionalSession$Intercepted.persist(Unknown Source)
    at pl.altkom.asc.lab.micronaut.poc.documents.infrastructure.adapters.db.PolicyDocumentDb.add(PolicyDocumentDb.kt:20)
    at pl.altkom.asc.lab.micronaut.poc.documents.domain.PolicyRegisteredListener.onPolicyRegistered(PolicyRegisteredListener.kt:24)
    at pl.altkom.asc.lab.micronaut.poc.documents.domain.$PolicyRegisteredListenerDefinition$$exec1.invokeInternal(Unknown Source)
    at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:146)
    at io.micronaut.core.bind.DefaultExecutableBinder$1.invoke(DefaultExecutableBinder.java:109)
    at io.micronaut.configuration.kafka.processor.KafkaConsumerProcessor.lambda$process$7(KafkaConsumerProcessor.java:498)
    at io.micronaut.scheduling.instrument.InvocationInstrumenterWrappedRunnable.run(InvocationInstrumenterWrappedRunnable.java:47)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
wojteksuwala commented 4 years ago

Thanks for reporting. I'll try to have a look at it during next weekend.

kingzevin commented 4 years ago

Thanks for the feedback.
The good news is that I just fixed it by adding the following lines to the pom.xml of documents-service.

        <dependency>
            <groupId>io.micronaut.data</groupId>
            <artifactId>micronaut-data-hibernate-jpa</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.data</groupId>
            <artifactId>micronaut-data-processor</artifactId>
            <scope>compile</scope>
        </dependency>

And the error disappeared.

wojteksuwala commented 4 years ago

could you provide a pull request?

kingzevin commented 4 years ago

OK. In the weekend I will.

wojteksuwala commented 4 years ago

Thanks for your help