question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reactive REST Client breaks Hibernate Reactive Transactions

See original GitHub issue

Describe the bug

When combining the Reactive REST client with Hibernate Reactive, the Reactive REST client seems to break Context Propagation, leading to Hibernate Transactions not working as intended.

Expected behavior

Using Reactive REST Client does not have any negative effects on Hibernate Reactive

Actual behavior

Using Reactive REST Client will break Hibernate Reactive Transactions because a call to sessionFactory.withSession() within an existing transaction will result in a new session being created instead of re-using the previously existing session residing in context.

How to Reproduce?

Reproducer: https://github.com/markusdlugi/hibernate-reactive-with-transaction (don’t get confused by the naming)

Steps to reproduce the behavior:

  1. Run the application
  2. Open http://localhost:8080/test. It will fail with java.lang.IllegalStateException: Couldn't find author 1
  3. Remove the REST call (TestResource line 48). If you repeat the test, it will now work, you get an author and some books.
  4. Revert your change and add the REST call again. The test will fail again.
  5. Remove the Hibernate Transaction by replacing .withTransaction((session, tx) in line 43 with .withSession(session. The test will now work again, even though the REST call is still in place.

It seems like after the REST call, the session2 in the reproducer is not the same session as session1 where the entity was initially persisted. This leads to the error. Seems like the Reactive REST client messes up the context propagation.

Not sure why the example works when getting rid of the transaction entirely, though.

Output of uname -a or ver

Microsoft Windows [Version 10.0.18363.1556]

Output of java -version

OpenJDK 64-Bit Server VM Corretto-11.0.10.9.1 (build 11.0.10+9-LTS, mixed mode)

GraalVM version (if different from Java)

N/A

Quarkus version or git rev

2.0.3.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.6.3

Additional information

Possibly related to #16949? But this one seems more severe because you cannot mix Hibernate Reactive Transactions and the REST client at all.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:31 (27 by maintainers)

github_iconTop GitHub Comments

6reactions
FroMagecommented, Aug 3, 2021

Actually, we get notified from the client on a VertxThread which has a non-null context, and then from that thread, we grab a random EventLoop thread instead of using the one we are on https://github.com/quarkusio/quarkus/blob/2.1.0.Final/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/RestClientRequestContext.java#L205 (remember there are one event loop thread per CPU) and execute the callback on that thread, which does not have a context. So it looks like our bug after all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactive Transactions with Spring
ReactiveTransactionManager is a transaction management abstraction for reactive and non-blocking integrations that uses transactional resources.
Read more >
Simplified Hibernate Reactive with Panache - Quarkus
Hibernate Reactive is the only reactive JPA implementation and offers you the full breadth of an Object Relational Mapper allowing you to access...
Read more >
Hibernate Reactive 1.0.3.Final Reference Documentation
Hibernate Reactive is a reactive API for Hibernate ORM, supporting non-blocking database drivers and a reactive style of interaction with ...
Read more >
Unanswered 'hibernate-reactive' Questions - Stack Overflow
I created a simple Spring/Vertx integration example application and add Hibernate Reactive to replace the existing Postgres Reactive Client API. Source codes: ...
Read more >
Avoiding Blocking Issues When Using Quarkus Reactive
This is provided by the hibernate-panache-reactive project, which brings Mutiny engine into hibernate-panache so it can allow developers to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found