Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hibernate Reactive with Panache: support stateless sessions #46091

Open
FroMage opened this issue Feb 5, 2025 · 12 comments
Open

Hibernate Reactive with Panache: support stateless sessions #46091

FroMage opened this issue Feb 5, 2025 · 12 comments

Comments

@FroMage
Copy link
Member

FroMage commented Feb 5, 2025

Description

As part of the work for #36168 I need to add support for stateless sessions in Hibernate Reactive with Panache.

Adding support in SessionOperations is easy enough, I can just duplicate most methods.

Figuring out what to do with @WithTransaction @WithSession and @WithLazySession is not entirely clear to me.

The problem is that in HR, transactions are tied to sessions, and so in order to obtain one TX we need to open a session, so pick a type.

Since it appears I can do in plain ORM:

@Transactional
public void foo(){
 MyEntity entity = new MyEntity();
 entity.managedBlocking().persist();
}
@Transactional
public void foo(){
 MyEntity entity = new MyEntity();
 entity.statelessBlocking().insert();
}

I would like to be able to achieve the same with HR/Panache:

@WithTransaction
public Uni<Void> foo(){
 MyEntity entity = new MyEntity();
 return entity.managedReactive().persist();
}
@WithTransaction
public void foo(){
 MyEntity entity = new MyEntity();
 return entity.statelessReactive().insert();
}

But ATM, @WithTransaction will open only a managed session.

I suppose it could open both sessions, but then we'd have two transactions, so that's not ideal.

Or we make a separate @WithStatelessTransaction but that's annoying.

We can definitely make @WithSession and @WithStatelessSession for explicit sessions.

I suppose we could make @WithLazySession open up laziness for both managed and stateless sessions at the same time.

I do wonder how ORM deals with these questions, I need to check.

Implementation ideas

No response

@FroMage FroMage added the kind/enhancement New feature or request label Feb 5, 2025
Copy link

quarkus-bot bot commented Feb 5, 2025

/cc @DavideD (hibernate-reactive), @gavinking (hibernate-reactive), @loicmathieu (panache)

@FroMage FroMage added area/panache and removed kind/enhancement New feature or request area/panache labels Feb 5, 2025
@FroMage
Copy link
Member Author

FroMage commented Feb 5, 2025

/cc @gavinking @DavideD @mkouba @yrodiere

@FroMage
Copy link
Member Author

FroMage commented Feb 5, 2025

Also, related, I need to ask @DavideD for a shared session interface for Mutiny.Session and Mutiny.StatelessSession similar to what SharedSessionContract has in ORM, with at least those operations:

    protected abstract <T> Uni<T> find(SessionType session, Class<T> entityClass, Object id);
    protected abstract <T> Uni<T> find(SessionType session, Class<T> entityClass, Object id, LockMode lockMode);
    protected abstract <R> Mutiny.SelectionQuery<R> createSelectionQuery(SessionType session, String var1, Class<R> var2);
    protected abstract <R> Mutiny.SelectionQuery<R> createNamedQuery(SessionType session, String var1, Class<R> var2);
    protected abstract <R> Mutiny.Query<R> createNamedQuery(SessionType session, String var1);
    protected abstract Mutiny.MutationQuery createMutationQuery(SessionType session, String var1);

I will open an issue in HR for that.

@DavideD
Copy link
Contributor

DavideD commented Feb 5, 2025

I will open an issue in HR for that.

Yes, please.

@yrodiere
Copy link
Member

yrodiere commented Feb 5, 2025

I suppose we could make @WithLazySession open up laziness for both managed and stateless sessions at the same time.

I do wonder how ORM deals with these questions, I need to check.

Laziness is where it's at. @Transactional won't start a transaction in the DB by itself. The transaction will only start after the ORM session opens its first connection -- and even there, I'd have to check, but possibly only when the first SQL statement is issued.

Something to note though, if you use both Session and StatelessSession in the same transaction, I think each session will have its own connection and thus, its own transaction. Might come as a surprise to some.

@FroMage
Copy link
Member Author

FroMage commented Feb 5, 2025

Laziness is where it's at. @Transactional won't start a transaction in the DB by itself. The transaction will only start after the ORM session opens its first connection -- and even there, I'd have to check, but possibly only when the first SQL statement is issued.

That makes a lot of sense. I guess we could do the same then.

Something to note though, if you use both Session and StatelessSession in the same transaction, I think each session will have its own connection and thus, its own transaction. Might come as a surprise to some.

Ah yes, I did wonder about that. This makes perfect sense to me, and aligns with what we can achieve in HR/Panache. So, good.

@yrodiere
Copy link
Member

yrodiere commented Feb 5, 2025

👍

Something to note though, if you use both Session and StatelessSession in the same transaction, I think each session will have its own connection and thus, its own transaction. Might come as a surprise to some.

Ah yes, I did wonder about that. This makes perfect sense to me, and aligns with what we can achieve in HR/Panache. So, good.

Uh, there was a mistake there: I think the sessions have their own connection, but possibly use the same transaction -- which will fail if XA is disabled, but will work if XA is enabled.

@FroMage
Copy link
Member Author

FroMage commented Feb 5, 2025

Ah, and I thought we did not support XA in Quarkus?

@yrodiere
Copy link
Member

yrodiere commented Feb 5, 2025

Ah, and I thought we did not support XA in Quarkus?

Not in Hibernate Reactive (AFAIK), but very much in Agroal / Hibernate ORM: https://quarkus.io/guides/datasource#datasource-multiple-single-transaction

@FroMage FroMage mentioned this issue Feb 5, 2025
@FroMage
Copy link
Member Author

FroMage commented Feb 5, 2025

Damnit.

@DavideD
Copy link
Contributor

DavideD commented Feb 6, 2025

@FroMage, I went ahead and created the issue for the shared contract: hibernate/hibernate-reactive#2104

@mkouba
Copy link
Contributor

mkouba commented Feb 17, 2025

Or we make a separate @WithStatelessTransaction but that's annoying.

@FroMage Or have something like @WithTransaction(stateless = true) . In any case, I don't think it's a good idea to open both sessions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants