1

I understand from https://github.com/quarkusio/quarkus/issues/32790 and the linked explanation that you can't have parallel unis reusing a session. From my understanding of it, you should be able to achieve the parallel aspect of it by having a different session for each Uni.

However, how is this supposed to work with Panache? Imagine the following example (I have other with two different PanacheQuery running in parallel but this is the simpler one to look at without much context):

    @WithSession
    public Uni<SearchSessionsResult> search(...) {
        final PanacheQuery<Session> query = this.find(logic with the received parameters)
        return Uni.combine()
            .all()
            .unis(
                query.list(), 
                query.pageCount(), 
                query.count()
            )
            .combinedWith(entities, pageCount, totalEntries) -> [some more business logic]);
    }

This will fail with java.lang.IllegalStateException: Illegal pop() with non-matching JdbcValuesSourceProcessingState becasue of the three unis running in parallel, which is expected. Since chaining the three unis is a bit impractical and doing things sequentially might have a big impact if you a bigger number of unis (and/or they are more complex), I wanted to follow the approach of having each uni run with a different session, but I'm unsure how to achieve this while still sticking to Panache

I tried wrapping the unis with Panache.withSession, like so:

    @WithSession
    public Uni<SearchSessionsResult> search(...) {
        final PanacheQuery<Session> query = this.find(logic with the received parameters)
        return Uni.combine()
            .all()
            .unis(
                Panache.withSession(query::list),
                Panache.withSession(query::pageCount),
                Panache.withSession(query::count)
            )
            .combinedWith(entities, pageCount, totalEntries) -> [some more business logic]);
    }

But the result is the same because Panache will reuse the current open reactive session for all unis. I also tried injecting the SessionFactory and opening sessions myself (I confess I don't know too much about how to do this, but I tried to follow the logic in the Panache class of opening the session and chaining a uni afterwards)


    @Inject
    SessionFactory sessionFactory;

    @WithSession
    public Uni<SearchSessionsResult> search(...) {
        final PanacheQuery<Session> query = this.find(logic with the received parameters)
        return Uni.combine()
            .all()
            .unis(
                sessionFactory.openSession().chain(session -> query.list().eventually(session::close)),
                sessionFactory.openSession().chain(session -> query.pageCount().eventually(session::close)),
                sessionFactory.openSession().chain(session -> query.count().eventually(session::close))
            )
            .combinedWith(entities, pageCount, totalEntries) -> [some more business logic]);
    }

And got the same result. So the question in essence is simple: How can I execute multiple DB queries in parallel using Panache? NOTE: The first example worked in Quarkus 2

  • just need to ask - what is your goal here? using separate sessions also means separate connections and separate transactions. Giving you performance overhead and possibly inconsistent data. – Max Rydahl Andersen Apr 30 '23 at 05:32
  • @MaxRydahlAndersen that's true indeed. My goal is not "use multiple sessions", that's just the path I think I'm forced to take. The goal is "how to do queries in parallel". In this case, to return a paginated list of entities, I need to know the entities themselves and info about the pages, so that on the next request the client can get the next page. I have another example where the client wants to associate many users with another entity, and I need to verify if the users exist first. If they send me 10 users, I'd have to chain 10 requests. Doing it in multiple sessions there would be ok – Tiago Grosso Apr 30 '23 at 08:04
  • This article has more inforative content related to this topic, and thought I would post here - https://blog.lunatech.com/posts/2023-05-19-hibernate-reactive-gotchas – Amaresh Kulkarni Jul 16 '23 at 20:33

0 Answers0