Skip to content

add operations for obtaining the current sessions from the context #2231

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

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,31 @@ default <T> Uni<T> withStatelessTransaction(Function<StatelessSession, Uni<T>> w
*/
Statistics getStatistics();

/**
* Return the current instance of {@link Session}, if any.
* A current session exists only when this method is called
* from within an invocation of {@link #withSession(Function)}
* or {@link #withTransaction(Function)}.
*
* @return the current instance, if any, or {@code null}
*
* @since 3.0
*/
Session getCurrentSession();

/**
* Return the current instance of {@link Session}, if any.
* A current session exists only when this method is called
* from within an invocation of
* {@link #withStatelessSession(Function)} or
* {@link #withStatelessTransaction(Function)}.
*
* @return the current instance, if any, or {@code null}
*
* @since 3.0
*/
StatelessSession getCurrentStatelessSession();

/**
* Destroy the session factory and clean up its connection pool.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ private CompletionStage<ReactiveConnection> connection(String tenantId) {
: connectionPool.getConnection( tenantId );
}

@Override
public Mutiny.Session getCurrentSession() {
return context.get( contextKeyForSession );
}

@Override
public Mutiny.StatelessSession getCurrentStatelessSession() {
return context.get( contextKeyForStatelessSession );
}

@Override
public <T> Uni<T> withSession(Function<Mutiny.Session, Uni<T>> work) {
Objects.requireNonNull( work, "parameter 'work' is required" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2273,6 +2273,31 @@ default <T> CompletionStage<T> withStatelessTransaction(Function<StatelessSessio
*/
Statistics getStatistics();

/**
* Return the current instance of {@link Session}, if any.
* A current session exists only when this method is called
* from within an invocation of {@link #withSession(Function)}
* or {@link #withTransaction(Function)}.
*
* @return the current instance, if any, or {@code null}
*
* @since 3.0
*/
Session getCurrentSession();

/**
* Return the current instance of {@link Session}, if any.
* A current session exists only when this method is called
* from within an invocation of
* {@link #withStatelessSession(Function)} or
* {@link #withStatelessTransaction(Function)}.
*
* @return the current instance, if any, or {@code null}
*
* @since 3.0
*/
StatelessSession getCurrentStatelessSession();

/**
* Destroy the session factory and clean up its connection pool.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ private CompletionStage<ReactiveConnection> connection(String tenantId) {
: connectionPool.getConnection( tenantId );
}

@Override
public Stage.Session getCurrentSession() {
return context.get( contextKeyForSession );
}

@Override
public Stage.StatelessSession getCurrentStatelessSession() {
return context.get( contextKeyForStatelessSession );
}

@Override
public <T> CompletionStage<T> withSession(Function<Stage.Session, CompletionStage<T>> work) {
Objects.requireNonNull( work, "parameter 'work' is required" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@
import jakarta.persistence.metamodel.EntityType;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;

@Timeout(value = 10, timeUnit = MINUTES)

Expand Down Expand Up @@ -668,6 +663,38 @@ public void testForceFlushWithDelete(VertxTestContext context) {
);
}

@Test
public void testCurrentSession(VertxTestContext context) {
test( context,
getMutinySessionFactory().withSession(session ->
getMutinySessionFactory().withSession(s -> {
assertEquals(session, s);
Mutiny.Session currentSession = getMutinySessionFactory().getCurrentSession();
assertNotNull(currentSession);
assertTrue(currentSession.isOpen());
assertEquals(session, currentSession);
return Uni.createFrom().voidItem();
}).invoke(() -> assertNotNull(getMutinySessionFactory().getCurrentSession()))
).invoke(() -> assertNull(getMutinySessionFactory().getCurrentSession()))
);
}

@Test
public void testCurrentStatelessSession(VertxTestContext context) {
test( context,
getMutinySessionFactory().withStatelessSession(session ->
getMutinySessionFactory().withStatelessSession(s -> {
assertEquals(session, s);
Mutiny.StatelessSession currentSession = getMutinySessionFactory().getCurrentStatelessSession();
assertNotNull(currentSession);
assertTrue(currentSession.isOpen());
assertEquals(session, currentSession);
return Uni.createFrom().voidItem();
}).invoke(() -> assertNotNull(getMutinySessionFactory().getCurrentStatelessSession()))
).invoke(() -> assertNull(getMutinySessionFactory().getCurrentStatelessSession()))
);
}

private void assertThatPigsAreEqual(GuineaPig expected, GuineaPig actual) {
assertNotNull( actual );
assertEquals( expected.getId(), actual.getId() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@Timeout(value = 10, timeUnit = MINUTES)

Expand Down Expand Up @@ -969,6 +964,42 @@ context, openSession()
);
}

@Test
public void testCurrentSession(VertxTestContext context) {
test( context,
getSessionFactory().withSession(session ->
getSessionFactory().withSession(s -> {
assertEquals(session, s);
Stage.Session currentSession = getSessionFactory().getCurrentSession();
assertNotNull(currentSession);
assertTrue(currentSession.isOpen());
assertEquals(session, currentSession);
return CompletionStages.voidFuture();
})
.thenAccept(v -> assertNotNull(getSessionFactory().getCurrentSession()))
)
.thenAccept(v -> assertNull(getSessionFactory().getCurrentSession()))
);
}

@Test
public void testCurrentStatelessSession(VertxTestContext context) {
test( context,
getSessionFactory().withStatelessSession(session ->
getSessionFactory().withStatelessSession(s -> {
assertEquals(session, s);
Stage.StatelessSession currentSession = getSessionFactory().getCurrentStatelessSession();
assertNotNull(currentSession);
assertTrue(currentSession.isOpen());
assertEquals(session, currentSession);
return CompletionStages.voidFuture();
})
.thenAccept(v -> assertNotNull(getSessionFactory().getCurrentStatelessSession()))
)
.thenAccept(v -> assertNull(getSessionFactory().getCurrentStatelessSession()))
);
}

private void assertThatPigsAreEqual(GuineaPig expected, GuineaPig actual) {
assertNotNull( actual );
assertEquals( expected.getId(), actual.getId() );
Expand Down
Loading