Skip to content

Commit d503cf3

Browse files
committed
[#1780] add operations for obtaining the current sessions from the context
this will make it easier to implement the necessary Quarkus integration
1 parent 29d84cd commit d503cf3

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,31 @@ default <T> Uni<T> withStatelessTransaction(Function<StatelessSession, Uni<T>> w
24712471
*/
24722472
Statistics getStatistics();
24732473

2474+
/**
2475+
* Return the current instance of {@link Session}, if any.
2476+
* A current session exists only when this method is called
2477+
* from within an invocation of {@link #withSession(Function)}
2478+
* or {@link #withTransaction(Function)}.
2479+
*
2480+
* @return the current instance, if any, or {@code null}
2481+
*
2482+
* @since 3.0
2483+
*/
2484+
Session getCurrentSession();
2485+
2486+
/**
2487+
* Return the current instance of {@link Session}, if any.
2488+
* A current session exists only when this method is called
2489+
* from within an invocation of
2490+
* {@link #withStatelessSession(Function)} or
2491+
* {@link #withStatelessTransaction(Function)}.
2492+
*
2493+
* @return the current instance, if any, or {@code null}
2494+
*
2495+
* @since 3.0
2496+
*/
2497+
StatelessSession getCurrentStatelessSession();
2498+
24742499
/**
24752500
* Destroy the session factory and clean up its connection pool.
24762501
*/

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinySessionFactoryImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ private CompletionStage<ReactiveConnection> connection(String tenantId) {
148148
: connectionPool.getConnection( tenantId );
149149
}
150150

151+
@Override
152+
public Mutiny.Session getCurrentSession() {
153+
return context.get( contextKeyForSession );
154+
}
155+
156+
@Override
157+
public Mutiny.StatelessSession getCurrentStatelessSession() {
158+
return context.get( contextKeyForStatelessSession );
159+
}
160+
151161
@Override
152162
public <T> Uni<T> withSession(Function<Mutiny.Session, Uni<T>> work) {
153163
Objects.requireNonNull( work, "parameter 'work' is required" );

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.hibernate.jpa.internal.util.FlushModeTypeHelper;
2727
import org.hibernate.proxy.HibernateProxy;
2828
import org.hibernate.query.Page;
29-
import org.hibernate.query.Query;
3029
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
3130
import org.hibernate.query.criteria.JpaCriteriaInsert;
3231
import org.hibernate.reactive.common.AffectedEntities;
@@ -2508,6 +2507,31 @@ default <T> CompletionStage<T> withStatelessTransaction(Function<StatelessSessio
25082507
*/
25092508
Statistics getStatistics();
25102509

2510+
/**
2511+
* Return the current instance of {@link Session}, if any.
2512+
* A current session exists only when this method is called
2513+
* from within an invocation of {@link #withSession(Function)}
2514+
* or {@link #withTransaction(Function)}.
2515+
*
2516+
* @return the current instance, if any, or {@code null}
2517+
*
2518+
* @since 3.0
2519+
*/
2520+
Session getCurrentSession();
2521+
2522+
/**
2523+
* Return the current instance of {@link Session}, if any.
2524+
* A current session exists only when this method is called
2525+
* from within an invocation of
2526+
* {@link #withStatelessSession(Function)} or
2527+
* {@link #withStatelessTransaction(Function)}.
2528+
*
2529+
* @return the current instance, if any, or {@code null}
2530+
*
2531+
* @since 3.0
2532+
*/
2533+
StatelessSession getCurrentStatelessSession();
2534+
25112535
/**
25122536
* Destroy the session factory and clean up its connection pool.
25132537
*/

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageSessionFactoryImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ private CompletionStage<ReactiveConnection> connection(String tenantId) {
138138
: connectionPool.getConnection( tenantId );
139139
}
140140

141+
@Override
142+
public Stage.Session getCurrentSession() {
143+
return context.get( contextKeyForSession );
144+
}
145+
146+
@Override
147+
public Stage.StatelessSession getCurrentStatelessSession() {
148+
return context.get( contextKeyForStatelessSession );
149+
}
150+
141151
@Override
142152
public <T> CompletionStage<T> withSession(Function<Stage.Session, CompletionStage<T>> work) {
143153
Objects.requireNonNull( work, "parameter 'work' is required" );

0 commit comments

Comments
 (0)