Skip to content

Lazy connection opening / transaction starting #2518

@yrodiere

Description

@yrodiere

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions