-
-
Notifications
You must be signed in to change notification settings - Fork 101
Description
I would like a way for Hibernate Reactive to open its connections lazily, instead of when the session gets created.
Motivation
First, opening the connection eagerly makes some patterns problematic: if the user opens a session, then starts some long-running external process, and only uses the session when that process finishes, they'll be holding a connection for a long time. Same goes for transactions.
That (among others, I'm sure) is the reason Hibernate ORM opens connections only when they are first needed, and I believe this holds for Hibernate Reactive.
Second, opening the connection on session creation means openSession
must return a Uni<Mutiny.Session>
(or CompletionStage
). This makes it hard (impossible?) to implement request-scoped or transaction-scoped sessions as described here.
To be precise, we could implement it, but users would be forced to first call a reactive method on the Session
before they can do anything else. Or to inject the Session
as a Uni<Session>
.
Implementation
I would suggest we expose a openSessionWithLazyConnection()
method (bikeshedding needed) on SessionFactory
, which would return a Session
instead of Uni<Session>
.
Internally, that session would only create a new connection when it first needs to access the database.
I believe the implementation would share a lot of ideas with #2494, in particular the idea of having a field in the session that represents the "last operation" -- in our case it would rather be the connection opening / transaction starting.