micronaut-sql icon indicating copy to clipboard operation
micronaut-sql copied to clipboard

Micronaut SQL Hibernate Maven fails with Could not obtain transaction-synchronized Session for current thread

Open sdelamo opened this issue 1 year ago • 9 comments

Steps to reproduce:

clone https://github.com/grails-core-issues-forks/micronaut-sql-1156

cd ../micronaut-jpa-hibernate-maven-java
./mvnw verify  

You will get:

18:50:44.274 [io-executor-thread-1] ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: 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.hibernate.MicronautSessionContext.currentSession(MicronautSessionContext.java:61)
	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:599)
	at io.micronaut.configuration.hibernate.jpa.TransactionalSessionInterceptor.intercept(TransactionalSessionInterceptor.java:62)
	at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:137)
	at io.micronaut.configuration.hibernate.jpa.TransactionalSession$Intercepted.persist(Unknown Source)
	at example.micronaut.GenreRepositoryImpl.save(GenreRepositoryImpl.java:40)

The same application works with Gradle.

cd micronaut-jpa-hibernate-gradle-java  
./gradlew test          

sdelamo avatar Nov 11 '23 17:11 sdelamo

@dstepanov do you know what it is going on?

sdelamo avatar Nov 11 '23 17:11 sdelamo

In maven, context propagation does not have transactions stored. Looks like adding this dependency to maven annotation processor

<path>
              <groupId>io.micronaut.data</groupId>
              <artifactId>micronaut-data-processor</artifactId>
              <version>${micronaut.data.version}</version>
            </path>

fixes the issue. The difference between gradle and maven is that gradle automatically adds micronaut-data-processor (which causes issue in https://github.com/micronaut-projects/micronaut-sql/issues/1157). Apparently it is needed for transaction handling via context propagation, but again @dstepanov probably knows why.

radovanradic avatar Jan 26 '24 16:01 radovanradic

That's coming from the Gradle plugin, which adds the processor when there is any kind of -data dependency.

dstepanov avatar Jan 29 '24 10:01 dstepanov

I see, but since data-tx-hibernate apparently needs data-processor then it should be added as dependency there so this issue wouldn't appear in micronaut-sql?

radovanradic avatar Jan 29 '24 10:01 radovanradic

It only needs it to remap the Jakarta transactional annotation. I think the solution should be to not reference enums in the metadata.

dstepanov avatar Jan 29 '24 10:01 dstepanov

This is different issue. Transactions are not stored/found in context propagation if there is no data-processor dependency which is what we have with Maven.

radovanradic avatar Jan 29 '24 10:01 radovanradic

Ah right. Disregard the last sentence, but the issue is that we remap different TX annotations to an internal one

dstepanov avatar Jan 29 '24 10:01 dstepanov

Checked with 4.3.2 and this still fails...

What's the solution here? We could add the data-processor to the annotation processor paths as specified here in starter?

timyates avatar Feb 21 '24 13:02 timyates

Than that will trigger this issue https://github.com/micronaut-projects/micronaut-sql/issues/1157 😞

radovanradic avatar Feb 21 '24 13:02 radovanradic